Communication System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23864 | Accepted: 8497 |
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
Source
Tehran 2002, First Iran Nationwide Internet Programming Contest
解题思路:
1.把数据按照产品编号:id,带宽:band,价格:price,存贮在结构体中,按照带宽、价格、编号有小到大排序;
2.枚举带宽:
按照带宽从小到达枚举,当枚举的带宽为第i个产品的带宽时,在剩下的n-1个产平的最大带宽必须 要大于第i个产品的带宽,如果不满足,产生一次剪枝。
保证大于枚举带宽值的带宽个数大于等于n-1,否则剪枝,因为,肯定不能有n个产品满足。
//2015-02-02 22:16:02
1 #include<iostream> 2 #include<cstdio>. 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 double ans; 9 struct product 10 { 11 int band; 12 int price; 13 int id; 14 }; 15 product pro[10007]; 16 int maxn[107]; 17 bool vis[107]; 18 int t,n,mi,m; 19 bool cmp(product p1,product p2) 20 { 21 if(p1.band!=p2.band) return p1.band<p2.band; 22 else if(p1.price!=p2.price) return p1.price<p2.price; 23 return p1.id<p2.id; 24 } 25 26 int main() 27 { 28 //freopen("in.txt","r",stdin); 29 scanf("%d",&t);int pri,cnt; 30 while(t--){ 31 ans=0; 32 scanf("%d",&n); 33 m=0; 34 memset(pro,0,sizeof(pro)); 35 memset(maxn,0,sizeof(maxn)); 36 for(int i=1;i<=n;i++) 37 { 38 scanf("%d",&mi); 39 for(int j=1;j<=mi;j++) 40 { 41 pro[m].id=i; 42 scanf("%d%d",&pro[m].band,&pro[m].price); 43 maxn[pro[m].id]=max(maxn[pro[m].id],pro[m].band); 44 m++; 45 } 46 } 47 sort(pro,pro+m,cmp); 48 for(int i=0;i<=m-n;i++) 49 { 50 memset(vis,0,sizeof(vis)); 51 vis[pro[i].id]=1; 52 cnt=1;bool flag=1; 53 pri=pro[i].price; 54 for(int j=i+1;j<m;j++) 55 { 56 if(vis[pro[j].id]) continue; 57 if(pro[i].band>maxn[pro[j].id]) 58 { 59 flag=0;break; 60 } 61 cnt++; 62 vis[pro[j].id]=1; 63 pri+=pro[j].price; 64 } 65 if(!flag||cnt<n) break; 66 ans=max(ans,pro[i].band*1.0/pri); 67 } 68 printf("%.3lf ",ans); 69 } 70 return 0; 71 }