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. 1 3
  2. 3 100 25 150 35 80 25
  3. 2 120 80 155 40
  4. 2 100 100 120 110

Sample Output

  1. 0.649

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
 
    题目大致意思:就是要做一个系统,然后需要n种配件,每种配件由于不同的厂家生产,它的参数 B (带宽)、P(价格)不一样,现在要选取这些配件,然后在n个配件中用最小的B除以所有的P的和,问怎么选才能使得B/P最大。
    题目的意思一开始没看明白,一直以为是用所有的B除以所有的P,结果手算总是算不对,搞得我都怀疑我的算数能力了。
    弄懂题目意思以后还是很容易做出来的。
    分析:
    1.我们可以先确定一个配件,以这个配件的B为n个配件中的最大B
    2.接着选后面的n-1个配件,满足要求:所选的配件的B不能大于第一个选择的配件的B
    3.后面的n-1种器件的P应该尽可能小
    4.选好以后,求出B/P的值,比较不同的选法那个B/P最大,输出最大的那个
 
代码如下:
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. struct goods
  7. {
  8. int p, b;
  9. };
  10. vector<goods>a[];
  11. const int MAXN = ;
  12.  
  13. void Clear() //用来清空信息的 很重要 没有这个会错
  14. {
  15. for (int i = ; i <= ; i++)
  16. a[i].clear();
  17. }
  18.  
  19. int main()
  20. {
  21. int t, num, n;
  22. double ans;
  23. goods a1, a2;
  24. cin >> t;
  25. while (t--)
  26. {
  27. Clear();
  28. ans = -;
  29. cin >> num; //器件总数
  30. for (int i = ; i < num; i++)
  31. {
  32. cin >> n;
  33. for (int j = ; j < n; j++)
  34. cin >> a1.b >> a1.p, a[i].push_back(a1);
  35. }
  36. for(int i = ; i < num; i++) //枚举第i个器件
  37. {
  38. for (int j = ; j < a[i].size(); j++) //枚举第i种器件的对应器件 确定第i种器件
  39. {
  40. a1 = a[i][j]; //选中第i种器件的第j个 而且假定这个器件的B是最小的
  41. double B = a1.b;
  42. double P = a1.p;
  43. int pp;
  44. for (int k = ; k < num; k++) //枚举n-1种器件
  45. {
  46. if (k == i) continue; //第i种器件已经选择过了 避免重复选择
  47. pp = MAXN;
  48. for (int h = ; h < a[k].size(); h++)
  49. {
  50. a2 = a[k][h];
  51. if (a2.b < B) continue; //假定B是最小的 不能选择更小的
  52. if (pp > a2.p) pp = a2.p; //为了使B/P尽可能大 P就要尽可能小
  53. }
  54. if (pp == MAXN) break; //找不到合适的器件 那么就表示选错了 重新选择
  55. P += pp;
  56. }
  57. if (pp == MAXN) break;
  58. if (ans < (B / P)) ans = (B / P);
  59. }
  60. }
  61. printf("%.3f\n", ans);
  62. }
  63. return ;
  64. }

POJ 1018 Communication System(贪心)的更多相关文章

  1. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  2. poj 1018 Communication System 枚举 VS 贪心

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21631   Accepted:  ...

  3. POJ 1018 Communication System(树形DP)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  4. poj 1018 Communication System

    点击打开链接 Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21007   Acc ...

  5. POJ 1018 Communication System (动态规划)

    We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...

  6. poj 1018 Communication System (枚举)

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted:  ...

  7. POJ 1018 Communication System(DP)

    http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...

  8. POJ 1018 Communication System 题解

    本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...

  9. poj 1018 Communication System_贪心

    题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...

随机推荐

  1. JAVA BASE64

    Base64编码说明:     Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式. 如果剩下的字符不足3个字节, ...

  2. 导入Mybatis_Spring项目遇到的问题

    1.  问题: jdk版本不匹配  解决方法:首先 到项目空间的   .setting文件中找到  org.eclipse.wst.common.project.facet.core.xml  修改参 ...

  3. python下划线作用初识

    单下划线(例:_textchar) 以单下划线做前缀的名称指定了这个名称是"私有的".在 有些 导入import * 的场景中,下一个使用你代码的人(或者你本人)会明白这个名称仅内 ...

  4. 几次面试后才弄懂的HashMap

    本人大四,以前也开发过几个项目,Map相关集合也总用.但是从来没有研究过底层的实现,只知道杂用.结果在最开始的几次面试中一脸懵逼.认识到不足后,浅显的学习了一下,总结成一下几点.(如果写错了还望指正) ...

  5. java for循环增强(foreach)

    for循环增强,在此之前还不知道foreach有这样的功能,先鄙视一下自己,留给自己看: 功能: ***若List用foreach : [  for(Student stu : list)  ]这种形 ...

  6. Tomcat+Servlet面试题都在这里

    下面是我整理下来的Servlet知识点:  图上的知识点都可以在我其他的文章内找到相应内容. Tomcat常见面试题 Tomcat的缺省端口是多少,怎么修改 Tomcat的缺省端口是多少,怎么修改 ...

  7. @RequestMapping注解

    Spring MVC中用于参数绑定的注解有很多,都在org.springframework.web.bind.annotation包中,根据它们处理的request的不同内容部分可以分为四类(主要讲解 ...

  8. @Controller注解

    Spring从2.5版本后开始引入注解,用户可以使用@Controller,@RequestMapping,@RequestParam,@ModelAttribute等类似这样的注解. @Contro ...

  9. spring之AspectJ基于xml AOP编程

    一.引言: AspectJ框架不仅实现了面向切面编程,而且还支持注解,spring将它引入自己的规范之中. 二.需要了解: AspectJ是基于java语言的AOP框架 spring2.0之后支持As ...

  10. File类中的list和listFiles方法

    File类中的list和listFiles方法 list()方法是返回某个目录下的所有文件和目录的文件名,返回的是String数组 listFiles()方法是返回某个目录下所有文件和目录的绝对路径, ...