题目如下,代码没有提交,不知道对不对,但是该实例跑出来的没有问题
Communication System
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22766 Accepted: 8098
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
c++代码如下,没有提交编译,不知道行不行,不过该实例是可以通过的
有人说该题可以用DP求解,没有想到这种方式,我用的是应该叫贪心算法吧?
代码片中有调试信息,并没有删除,主要为了观看一下输出结果
#include <iostream>
#include <vector>
#include <string.h>
#include <limits.h>
using namespace std;
struct Node{
int bandwidth;
int price;
//构造函数
Node():bandwidth(0),price(0){}
Node(int b=0,int p=0):bandwidth(b),price(p){}
};
//完成该任务的函数
void communicationSystem();
int main()
{
int num_samples=0;
cin>>num_samples;
while(num_samples--){
communicationSystem();
}
}
void communicationSystem()
{
//分别用于存储带宽、价格
vector<Node> samples;
vector<Node> BP;
int num_devices=0,num_manufactures=0;
cin>>num_devices;
while(num_devices--){
cin>>num_manufactures;
int i=0,num_bandwidth=0,num_price=0;
double greater=0.0f;
int index=0;
for(i=0;i<num_manufactures;i++){
cin>>num_bandwidth>>num_price;
if(((num_bandwidth+0.0)/num_price)>greater){
greater=(num_bandwidth+0.0)/num_price;
index=i;
}
samples.push_back(Node(num_bandwidth,num_price));
}
//cout<<"greater="<<greater<<",index= "<<index<<endl;
BP.push_back(samples[index]);
//clear
samples.clear();
}
for(int i=0;i<samples.size();++i){
cout<<samples[i].bandwidth<<" "<<samples[i].price<<endl;
}
cout<<"CANDIDATE:\n";
int min=INT_MAX,sum=0;
for(int i=0;i<BP.size();++i){
cout<<BP[i].bandwidth<<" "<<BP[i].price<<endl;
if(min>BP[i].bandwidth)min=BP[i].bandwidth;
sum+=BP[i].price;
}
cout.precision(3);
cout<<"min="<<min<<",sum="<<sum<<",result:"<<(min+0.0)/sum<<endl;
}