POJ 1018 Communication System

Communication System
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20407   Accepted: 7253

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

// 搜索解决
// 枚举b ,然后算出b/sum{pi},求出最大的
#include <iostream> #include <math.h> #include <vector> #include <queue> #include <stack> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N=110; struct P { int b,p; bool operator <(const P & temp) const { return b>temp.b; } }in[N][N]; int Min[N][N]; int num[N]; int b[N*N]; int n; int solve(int i,int v) //这个递归还可以边递归边算,如果算到答案比已经算出的答案小,就不必继续递归下去了 { if(i==n) return 0; int j; for(j=num[i]-1;j>=0;j--) //这里可以用二分查找继续优化 if(in[i][j].b>=v) break; if(j>=0) { int temp=solve(i+1,v); if(temp>=0) return temp+Min[i][j]; return -1; } return -1; } int main() { int t; scanf("%d",&t); int i,j,k; while(t--) { scanf("%d",&n); k=0; for(i=0;i<n;i++) { scanf("%d",&num[i]); for(j=0;j<num[i];j++) scanf("%d %d",&in[i][j].b,&in[i][j].p),b[k++]=in[i][j].b; sort(in[i],in[i]+num[i]);//对 b从大到小排序,为搜索的便利作准备 Min[i][0]=in[i][0].p; for(j=1;j<num[i];j++) //从开始到相应位置的 算出最小的p Min[i][j]=Min[i][j-1]<in[i][j].p?Min[i][j-1]:in[i][j].p; } sort(b,b+k); int sum=0; double temp=b[0],ans; ans=temp/solve(0,b[0]); for(i=0,j=1;j<k;j++) //离散化优化,避免相同的b被枚举 if(b[i]!=b[j]) { b[++i]=b[j]; sum=solve(0,b[i]); if(sum>0) { temp=sum; ans=ans>b[i]/temp?ans:b[i]/temp; } } printf("%.3lf\n",ans); } return 0; }

 

posted on 2013-05-16 17:37  江财小子  阅读(486)  评论(0编辑  收藏  举报