码迷,mamicode.com
首页 > 其他好文 > 详细

poj_1018_多种方法求解

时间:2014-05-20 07:37:17      阅读:454      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   c   code   

                                                          Communication System
 
 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22445   Accepted: 7977

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


首先还是要感谢yiyi教我这道题。这道题可以用三分,DP,枚举来做,目前只写了枚举,而且也只会枚举,未完,待续。

先来探讨一下对这道题的想法。乍看这道题,感觉是用贪心来做,但是贪心的缺陷:步步最优不一定是整体最优,就在这道题里体现了出来。如下面的数据:

1 2
2 10 3 5 1
2 10 2 10 1

  如果按照贪心的思路,会选择 5 1 和 10 1 两组数据,其最终结果为 5/(1+1) = 2.500;可是很明显,如果取 10 3 和 10 1 ,结果 10/(3+1) = 4.000 ,更优。。。因此,贪心的思路不可取。

  先不考虑P的取值,找到有可能的B的范围:每组最小的B中最小的B 到 每组最大的B中最小的B,所有可能的B的值必然介于其中。(提示:B的值取决于最小的B) 这样,有了B的范围,P的值也随之确定。

  为了便于处理,可以将所有有可能的B可以放到一个数组里面。下面是枚举的AC代码:

 

bubuko.com,布布扣
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 #include<cmath>
 7 
 8 using namespace std;
 9 
10 int cmp(int a, int b) {
11     return a > b;
12 }
13 
14 struct band
15 {
16     int w;
17     int p;
18 } ;
19 
20 band d[105][105];
21 
22 int m[105];
23 int temp[105];
24 int bb[10005];
25 int co, flag, t, p;
26 int i, j, k, l;
27 int T, n, tt, pp;
28 
29 double P, ans;
30 
31 int main()
32 {
33     scanf("%d", &T);
34 
35     while(T--) {
36         co = 0;
37         ans = 0;
38         tt = 0x7fffffff;
39         pp = 0x7fffffff;
40 
41         scanf("%d", &n);
42 
43         for(i = 0; i < n; i++) {
44             p = 0;
45             t = 0x7fffffff;
46             scanf("%d", &m[i]);
47 
48             for(j = 0; j < m[i]; j++) {
49 
50                 scanf("%d%d", &d[i][j].w, &d[i][j].p);
51                 bb[co++] = d[i][j].w;
52                 t = min(t, d[i][j].w);
53                 p = max(p, d[i][j].w);
54             }
55             tt = min(tt, t);
56             pp = min(pp, p);
57         }
58         sort(bb, bb+co, cmp);
59         for(i = 0; i < co; i++) {
60             if(bb[i] >= tt && bb[i] <= pp && bb[i] != bb[i+1]) {
61                 P = 0;
62                 for(k = 0; k < n; k++) {
63 
64                     double mi = 1e10;
65 
66                     for(l = 0; l < m[k]; l++) {
67                         if(d[k][l].w >= bb[i] && d[k][l].p < mi)
68                         mi = d[k][l].p;
69                     }
70                     P += mi;
71                 }
72                 ans = max(ans, bb[i]/P);
73             }
74         }
75         printf("%.3lf\n", ans);
76     }
77     return 0;
78 }
bubuko.com,布布扣

 




poj_1018_多种方法求解,布布扣,bubuko.com

poj_1018_多种方法求解

标签:des   style   blog   class   c   code   

原文地址:http://www.cnblogs.com/sayeter/p/3735691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!