首页 > 代码库 > poj 1018(枚举+贪心)

poj 1018(枚举+贪心)

 
                                                                        通讯系统
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
题意:系统需要n件设备,每件设备可以有m个厂家生产,但宽度个价格会存在差别,现在每种设备都需要一个,
题意要求的是满足(B/P)max最大的情况下选出这n件设备,
B为这n件设备的最小宽度,P为花费的价格

我们在这所有设被备从小到大枚举设备的宽度,找出它对应的最小的价格,最后区最大的q/p;
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<cstdlib>
11 #include<string>
12 #define eps 0.000000001
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const int N=100+10;
17 double Max(double a,double b){
18     return a>b?a:b;
19 }
20 int B[N][N],P[N][N];
21 int a[N];
22 int f(int t,int n){
23     for(int i=1;i<=n;i++)
24     for(int j=1;j<=a[i];j++)
25     if(B[i][j]==t)return 1;
26     return 0;
27 }
28 int main(){
29     int t,n;
30 
31     scanf("%d",&t);
32     while(t--){
33         int minn=0x3f3f3f3f;
34         int maxx=-0x3f3f3f3f;
35         scanf("%d",&n);
36         for(int i=1;i<=n;i++){
37             scanf("%d",&a[i]);
38             for(int j=1;j<=a[i];j++){scanf("%d%d",&B[i][j],&P[i][j]);
39             minn=min(B[i][j],minn);
40             maxx=max(B[i][j],maxx);
41             }
42         }
43        // cout<<minn<<" "<<maxx<<endl;
44         double ans=0.0;
45         for(int i=minn;i<=maxx;i++){
46             if(f(i,n)==0)continue;
47             int sum=0;
48             for(int j=1;j<=n;j++){
49                 int t=0x3f3f3f3f;
50                 for(int k=1;k<=a[j];k++){
51                     if(B[j][k]>=i)t=min(t,P[j][k]);
52                 }
53                 sum=sum+t;
54             }
55             //cout<<i<<" "<<sum<<endl;
56             ans=Max(ans,(double)i/sum);
57         }
58         printf("%.3f\n",ans);
59     }
60 }

 



poj 1018(枚举+贪心)