Communication System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22424 | Accepted: 7970 |
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
参考了代码
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string.h>
using namespace std;
class info{
public:
int B;
double P;
int id;
};
int cmp(const void*a,const void*b){
info*x=(info*)a;
info*y=(info*)b;
if((x->B)==(y->B))//当带宽相等时
{
if((x->P)==(y->P))//当价格也相等时
return (x->id)-(y->id);//以编号为第三优先升序排序
return (x->P)-(y->P); //以价格为第二优先升序排序
}
return (x->B)-(y->B); //以带宽为第一优先升序排序
}
double max(double a,double b){
return a>b?a:b;
}
int main(int i,int j){
int test;
cin>>test;
for(int t=0;t<test;t++){
int n;
int m=0;
cin>>n;
int* MaxB=new int[n+1];
info* dev=new info[100*100+1];
int pd=0;
for(i=0;i<n;i++){
int mi;
cin>>mi;
m+=mi;
MaxB[i]=-1;
for(j=0;j<mi;j++){
pd++;
cin>>dev[pd].B>>dev[pd].P;
dev[pd].id=i;
MaxB[i]=max(MaxB[i],dev[pd].B);
}
}
qsort(dev,m+1,sizeof(info),cmp);
bool flag=false;
double ans=0;
for(i=0;i<m-(n-1);i++){//枚举所有设备带宽的最小带宽B
//m-(n-1)是剪枝,因为当设备数>生产商数时就不必枚举了
bool* vist=new bool[n+1];
memset(vist,false,sizeof(bool)*(n+1));
vist[dev[i].id]=true;
double price=dev[i].P;//设备总价
int count=1;
for(j=i;j<m;j++){
if(vist[dev[j].id])
continue;
if(dev[i].B>MaxB[dev[j].id]){ //剪枝
flag=true; //当前枚举的 "所有设备带宽的最小带宽Bi" 比 "设备j的最大带宽MaxBj" 要大
break; //说明当前Bi已经越界,无需继续往后枚举
}
vist[dev[j].id]=true;
price+=dev[j].P;
count++;
}
if(flag||count<n){
break;
}
ans=max(ans,(dev[i].B/price));
}
cout<<fixed<<setprecision(3)<<ans<<endl;
delete MaxB;
delete dev;
}
return 0;
}
/*1 3
3 100 25 150 35 80 25
2 120 80 155 40
2 100 100 120 110*/