Communication System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29630 | Accepted: 10539 |
Description
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.
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 题目大意: 有n种设备,每种设备有多种选择,同种设备的不同选择中给出每种选择的带宽和价格,现在要从这些设备中的不同 选择中选出一种方案,使得带宽和与价格和的比值最大。 解题思路: 只用求出价格的最小值以及带宽的最大值,那比值一定最大。题目中的带宽数据比较小,因此可以用动态规划,用 s[i][j]中的j表示带宽,s[i][j]中存入价格,由动态转移方程s[i][j]=min(s[i][j],s[i-1][j]+p)来计算所有带宽 和的情况以及其所对应的价格和,之后求出比值最大即可。 代码:#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; typedef struct stu{ int x ,y; }st; #define inf 99999999 st s[110][1010]; int s1[110][1010]; int main() { int i,j,k,m,n,x,y,t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=n;i++) for(j=1;j<=1010;j++) s1[i][j]=inf; for(i=1;i<=n;i++) { scanf("%d",&m); for(j=1;j<=m;j++) { scanf("%d%d",&s[i][j].x,&s[i][j].y); for(k=0;k<=1010;k++) { if(s[i][j].x>k) s1[i][k]=min(s1[i][k],s1[i-1][k]+s[i][j].y); else s1[i][s[i][j].x]=min(s1[i][s[i][j].x],s1[i-1][s[i][j].x]+s[i][j].y); } } } double v; v=0; for(j=1;j<=1010;j++) v=max((double) j/s1[n][j],v); printf("%.3lf\n",v); } return 0; }