链接:http://poj.org/problem?id=1018
题目大意:有n个设备,每个设备有mi个制造商,每个设备选一个制造商,使最后选择的n个制造商的B/p值最大,b是选择的最小b值,p为所有选择的p值得总和
题目思路:
搜索超时,贪心解决;
原题:
Communication System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 19857 | Accepted: 7042 |
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<stdio.h>
using namespace std;
struct node
{
double b,p;
}manu[105][105];
int dev[105];
double aveb,avep;
double aans[105];
int p=0;
int n;
void dfs(int i)
{
if(i==n)
{
aans[p++]=aveb/avep;
return ;
}
double tmp=aveb;
for(int j=0;j<dev[i];j++)
{
aveb=min(tmp,manu[i][j].b);
avep+=manu[i][j].p;
dfs(i+1);
aveb=tmp;
avep-=manu[i][j].p;
}
}
int main()
{
int t;
scanf("%d",&t);
p=0;
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&dev[i]);
for(int j=0;j<dev[i];j++)
{
scanf("%lf%lf",&manu[i][j].b,&manu[i][j].p);
}
}
aveb=0;
avep=0;
for(int i=0;i<dev[0];i++)
{
aveb=manu[0][i].b;
avep+=manu[0][i].p;
dfs(1);
avep-=manu[0][i].p;
}
double maxx=-1.0;
for(int i=0;i<p;i++)
{
//printf("%.3lf ",aans[i]);
if(maxx<aans[i])
maxx=aans[i];
}
// printf("\n");
printf("%.3lf\n",maxx);
}
return 0;
}
代码(贪心):
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node
{
int b,p;
}manu[105][105];
int dev[105];
int aveb,avep;
double aans;
int n;
int cmp(struct node a,struct node b)
{
return a.p<b.p;
}
int main()
{
int t;
int k,pp;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&dev[i]);
for(int j=0;j<dev[i];j++)
{
scanf("%d%d",&manu[i][j].b,&manu[i][j].p);
}
sort(&manu[i][0],&manu[i][0]+dev[i],cmp);
}
aveb=0;
avep=0;
aans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<dev[i];j++)
{
aveb=manu[i][j].b;
avep=manu[i][j].p;
for(k=0;k<n;k++)
{
if(k==i)
continue;
for(pp=0;pp<dev[k];pp++)
{
if(aveb<=manu[k][pp].b)
break;
}
if(pp>=dev[k])
break;
avep+=manu[k][pp].p;
}
if(k<n)
continue;
if(aans<aveb*1.0/avep*1.0)
aans=aveb*1.0/avep*1.0;
}
}
printf("%.3f\n",aans);
}
return 0;
}