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
 
    题目大致意思:就是要做一个系统,然后需要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最大,输出最大的那个
 
代码如下:
#include <iostream>
#include <vector> using namespace std; struct goods
{
int p, b;
};
vector<goods>a[];
const int MAXN = ; void Clear() //用来清空信息的 很重要 没有这个会错
{
for (int i = ; i <= ; i++)
a[i].clear();
} int main()
{
int t, num, n;
double ans;
goods a1, a2;
cin >> t;
while (t--)
{
Clear();
ans = -;
cin >> num; //器件总数
for (int i = ; i < num; i++)
{
cin >> n;
for (int j = ; j < n; j++)
cin >> a1.b >> a1.p, a[i].push_back(a1);
}
for(int i = ; i < num; i++) //枚举第i个器件
{
for (int j = ; j < a[i].size(); j++) //枚举第i种器件的对应器件 确定第i种器件
{
a1 = a[i][j]; //选中第i种器件的第j个 而且假定这个器件的B是最小的
double B = a1.b;
double P = a1.p;
int pp;
for (int k = ; k < num; k++) //枚举n-1种器件
{
if (k == i) continue; //第i种器件已经选择过了 避免重复选择
pp = MAXN;
for (int h = ; h < a[k].size(); h++)
{
a2 = a[k][h];
if (a2.b < B) continue; //假定B是最小的 不能选择更小的
if (pp > a2.p) pp = a2.p; //为了使B/P尽可能大 P就要尽可能小
}
if (pp == MAXN) break; //找不到合适的器件 那么就表示选错了 重新选择
P += pp;
}
if (pp == MAXN) break;
if (ans < (B / P)) ans = (B / P);
}
}
printf("%.3f\n", ans);
}
return ;
}

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. php 5.0 与7.0有什么区别

    我有更好的答案 发布于2017-05-19 12:30 最佳答案 PHP7特性 PHP 7.0.0 Alpha 1[1] 使用新版的ZendEngine引擎,带来了许多新的特性,以下是不完全列表: 性 ...

  2. Java hashtable和hastmap的区别

    1. 继承和实现区别 Hashtable是基于陈旧的Dictionary类,完成了Map接口:HashMap是Java 1.2引进的Map接口的一个实现(HashMap继承于AbstractMap,A ...

  3. eclipse导入web项目变成java项目解决办法

    右键工程,properties-> Project Facets-> 点convert to faceted..连接 -> 把Dynamic Web Moudle勾上

  4. linux的nvme驱动需要关心的统计项

    blk-mq-sysfs.c生成了一些其他的nvme的统计项, 有多少个online的cpu,在驱动加载的时候会默认生成多少个队列,除非内存不足或者在保留内核中,则会减少. [root@localho ...

  5. [Git] git log命令

    这是git的新系列,不常用的命令和其参数比较容易记不住,干脆将常用的记录下来,日后查查方便也是好的,一篇文章一个git命令,长短根据命令有所不同. git log命令主要用于查看提交历史,同时根据添加 ...

  6. Windows核心编程&线程

    1. 线程上下文:线程内核对象保存线程上一次执行时的CPU寄存器状态 2. 线程上下文切换 3. windows操作系统为抢占式多线程操作系统,系统可以在任何时刻停止一个线程而另行调度另外一个线程.我 ...

  7. java注解--Annotation

    Annotation(注释) 概述 从 JDK 5.0 开始, Java 增加了对元数据(MetaData) 的支持, 也就是 Annotation(注释) Annotation 其实就是代码里的特殊 ...

  8. MySQL权限详解

    MySQL权限级别介绍 MySQL权限级别 全局性的管理权限,作用于整个MySQL实例级别 数据库级别的权限,作用于某个指定的数据库上或者所有的数据库上 数据库对象级别的权限,作用于指定的数据库对象上 ...

  9. 网卡bond技术

    概览: 目前网卡绑定mode共有七种(0~6)bond0.bond1.bond2.bond3.bond4.bond5.bond6 常用的有三种: mode=0:平衡负载模式,有自动备援,但需要&quo ...

  10. [PHP]接口请求校验的原理

    具体的校验步骤可以自定义,下面是比较直观的一种形式: 1. 客户端:请求参数带上时间,进行首字母排序,连接私钥后,取得加密结果: 客户端请求时带上这个加密结果作为sign参数. 2. 服务端:对sig ...