We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we are free to choose from several manufacturers. Same devices from two manufacturers differ in their maximum bandwidths and prices. 
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen devices in the communication system and the total price (P) is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P. 

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case starts with a line containing a single integer n (1 ≤ n ≤ 100), the number of devices in the communication system, followed by n lines in the following format: the i-th line (1 ≤ i ≤ n) starts with mi (1 ≤ mi ≤ 100), the number of manufacturers for the i-th device, followed by mi pairs of positive integers in the same line, each indicating the bandwidth and the price of the device respectively, corresponding to a manufacturer.

Output

Your program should produce a single line for each test case containing a single number which is the maximum possible B/P for the test case. Round the numbers in the output to 3 digits after decimal point. 

Sample Input

1 3
3 100 25 150 35 80 25
2 120 80 155 40
2 100 100 120 110

Sample Output

0.649

题解:我们定义状态dp 【i】【j】 表示选择了前 i 个宽带其容量为 j 的最小费用

很容易得到转移方程 :dp【i】【j】=min(dp【i】【j】,dp【i-1】【k】+p);注意选择 j 的时候的大小情况
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
return x>y;
}
const int N=;
const int mod=1e9+;
const int inf = 0x3f3f3f3f;
int dp[][];
int main()
{
int t;
scanf("%d",&t);
while(T--){
int n;
scanf("%d",&n);
for(int i=;i<=n;i++){
for(int j=;j<;j++)
dp[i][j]=inf;
}
for(int i=; i<=n; i++) {
int num;
scanf("%d",&num);
for(int j=; j<=num;j++){
int p,b;
scanf("%d%d",&b,&p);
if(i==){
dp[][b]=min(dp[][b],p);
}
else{
for(int k=;k<;k++){
if(dp[i-][k]!=inf){
if(k<=b)
dp[i][k]=min(dp[i][k],dp[i-][k]+p);
else
dp[i][b]=min(dp[i][b],dp[i-][k]+p);
}
}
}
}
}
double ans=;
for(int i=;i<;i++){
if(dp[n][i]!=inf){
double k=(double)i/dp[n][i];
if(k>ans)
ans=k;
}
}
printf("%.3lf\n",ans);
}
return ;
}

POJ 1018 Communication System (动态规划)的更多相关文章

  1. POJ 1018 Communication System(树形DP)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  2. poj 1018 Communication System

    点击打开链接 Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21007   Acc ...

  3. poj 1018 Communication System 枚举 VS 贪心

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21631   Accepted:  ...

  4. POJ 1018 Communication System(贪心)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  5. poj 1018 Communication System (枚举)

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted:  ...

  6. POJ 1018 Communication System(DP)

    http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...

  7. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  8. POJ 1018 Communication System 题解

    本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...

  9. poj 1018 Communication System_贪心

    题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...

随机推荐

  1. MySQL中drop,delete与truncate的区别

    drop直接删掉表 truncate删除表中数据,再插入时自增长id又从1开始 delete删除表中数据,可以加where字句. (1) DELETE语句执行删除的过程是每次从表中删除一行,并且同时将 ...

  2. what's the python之字符编码与文件处理

    用文本编辑器打开一个文件就是把一个文件读入了内存中 ,所以打开文件的操作也是在内存中的,断电即消失,所以若要保存其内容就必须点击保存让其存入硬盘中 python解释器执行py文件的原理 : 第一阶段: ...

  3. MSDN、OEM、VOL、RETAIL密钥区别

    本文就介绍一下Windows的密钥的一些使用要点及注意事项,涉及到最常用的MSDN密钥.OEM密钥.VOL密钥和零售密钥激活问题,希望对大家有所帮助. 一.MSDN密钥 MSDN密钥是付费用户提前获得 ...

  4. sqlite基本操作

    sqlite准备步骤; .下载:https://www.sqlite.org/download.html: sqlite-dll-win64-3250200.zip 和 sqlite-tools-wi ...

  5. 阿里云ECS利用密钥对ssh登录服务器

    https://blog.csdn.net/u012865381/article/details/78521087/ 1.在服务机上操作创建要远程登录的用户和密码 [root@izwz97s23bov ...

  6. 异常Exception分类

    1:编译时被检测异常:只要有是Exception和其子类都是,除了特殊子类RuntimeException体系.       这种问题已但出现,希望在编译时进行检测,让这种问题有对应处理方式      ...

  7. composer install 遭遇404错误

    [Composer\Downloader\TransportException] The "https://packagist.phpcomposer.com/p/provider-2019 ...

  8. js模拟栈---汉诺塔

    var Stack = (function(){ var items = new WeakMap(); //先入后出,后入先出 class Stack{ constructor(){ items.se ...

  9. Node.js进击基础一(http)

    URL:统一资源定位符,偏重定位,是URI的子集,例如网址.URL一定是URI,但URI 不一定是URL.规则:只能用英文阿拉伯数字某些符号等,如果有文字就必须编码. URI:统一资源标识符,偏重标识 ...

  10. iOS LeftMenu抽屉效果与ScrollView共存时的手势冲突

    公司有个项目,需要做左侧滑动,首页是ScrollView嵌套TableView.首页是一个ScrollView,所以当contentOffset是0.0的时候,无法直接滑动出抽屉效果,用户体验感非常差 ...