首页 > 代码库 > poj 1018 Communication System (枚举)

poj 1018 Communication System (枚举)

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

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. 

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

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
 
投降的题...
 
题意:
    给出n样零件,及每样零件的都有b[i]个厂生产,有带宽b和价格p两个值。求全部零件(每样一件) min(b)/sum(p) 的最大值。
 
枚举:
    对带宽进行枚举,选出全部零件的最大及最小带宽,然后进行枚举,每个循环选出符合条件的价格的和,得出ans再进行比较,得到最优解。
 1 //232K    47MS    C++    1164B    2014-05-07 19:16:42
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h> 
 5 struct node{
 6     int b;
 7     int p;
 8 }a[105][105];
 9 int b[105];
10 int n;
11 inline int Max(int x,int y)
12 {
13     return x>y?x:y;
14 }
15 inline int Min(int x,int y)
16 {
17     return x<y?x:y;
18 }
19 int main(void)
20 {
21     int t,m;
22     scanf("%d",&t);
23     while(t--)
24     {
25         scanf("%d",&n);
26         int ln=0x7ffffff,rn=0;
27         for(int i=0;i<n;i++){
28             scanf("%d",&b[i]);
29             for(int j=0;j<b[i];j++){
30                 scanf("%d%d",&a[i][j].b,&a[i][j].p);
31                 ln=Min(ln,a[i][j].b);
32                 rn=Max(rn,a[i][j].b);
33             }
34         }
35         double ans=0;
36         for(int i=ln;i<=rn;i++){
37             int sminn=0;
38             for(int j=0;j<n;j++){
39                 int minn=0x7ffffff;
40                 for(int k=0;k<b[j];k++){
41                     if(a[j][k].b>=i && minn>a[j][k].p)
42                         minn=a[j][k].p;
43                 }
44                 sminn+=minn;
45             }
46             ans=ans>(i*1.0/sminn)?ans:(i*1.0/sminn);
47             //printf("*****%lf\n",ans);
48         }   
49         printf("%.3lf\n",ans);
50     }
51     return 0;
52 }