POJ 1018 Communication System
Communication System
Time Limit: 1000MS |
Memory Limit: 10000K |
|
---|---|---|
Total Submissions: 28182 |
Accepted: 10049 |
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
题目链接:http://poj.org/problem?id=1018
题意:
某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1、m2、m3、...、mn个厂家提供生产,而每个厂家生产的同种设备都会存在两个方面的差别:带宽bandwidths 和 价格prices。
现在每种设备都各需要1个,考虑到性价比问题,要求所挑选出来的n件设备,要使得B/P最大。
其中B为这n件设备的带宽的最小值,P为这n件设备的总价。
分析:此题目可用多种方法求解,DP 、 搜索 、贪心 、三分法
这里讲dp的思路。
我们定义状态dp 【i】【j】 表示选择了前 i 个宽带其容量为 j 的最小费用。
很容易得到转移方程 :dp【i】【j】=min(dp【i】【j】,dp【i-1】【k】+p);
注意选择 j 的时候的大小情况。
顺便提供一下贪心的思路。(正确性未知)
从初始的第一个要选的宽带的每一个开始,每次向下贪心选择一个总的 B/P 的最大值,找出其中最大的既为答案。有兴趣的可以验证一下正确性!
dp代码:
1 #include <iostream>
2 #include <cstdio>
3 #include <queue>
4 #include <iomanip>
5 using namespace std;
6
7 const int maxn=1100;
8 const int inf=0x3f3f3f3f;
9 int dp[maxn];
10
11 struct node{
12 int flow,sum;
13 node(int flow0=0,int sum0=0){
14 flow=flow0,sum=sum0;
15 }
16 };
17
18 void solve(){
19 queue <node> q;
20 q.push(node(inf,0));
21 int n,m,flow,price;
22 scanf("%d",&n);
23 for(int i=1;i<=n;i++){
24 scanf("%d",&m);
25 for(int i=0;i<maxn;i++) dp[i]=inf;
26 while(m-- >0){
27 scanf("%d%d",&flow,&price);
28 int qsize=q.size();
29 while(qsize-- >0){
30 node s=q.front();
31 q.pop();
32 if(s.flow<flow){
33 if(s.sum+price<dp[s.flow]) dp[s.flow]=s.sum+price;
34 }else{
35 if(s.sum+price<dp[flow]) dp[flow]=s.sum+price;
36 }
37 q.push(s);
38 }
39 }
40 while(!q.empty()) q.pop();
41 for(int i=0;i<maxn;i++){
42 if(dp[i]<inf) q.push(node(i,dp[i]));
43 }
44 }
45 double ans=0;
46 while(!q.empty()){
47 node s=q.front();
48 q.pop();
49 if(double(s.flow)/double(s.sum) > ans) ans= double(s.flow)/double(s.sum) ;
50 }
51 cout<<setiosflags(ios::fixed)<<setprecision(3)<<ans<<endl;
52 }
53
54 int main(){
55 int t;
56 scanf("%d",&t);
57 while(t-- >0){
58 solve();
59 }
60 return 0;
61 }
- 浅谈应用型机器学习作为一种搜索问题
- 自相关和偏自相关的简单介绍
- 机器学习中分类与回归的差异
- 自然语言处理指南(第1部分)
- GreenDao 兼容升级,保留旧数据的---全方面解决方案
- 基于 xorm 的服务端框架 XGoServer
- 全面总结: Golang 调用 C/C++,例子式教程
- 架构之路(六):把框架拉出来
- 第二届游戏运营技术论坛——云时代的游戏运营之道
- 如何才能准确测量 APP 的功耗?
- 可用性更高:设计优秀的MySQL和Percona XtraDB集群
- 如何使用scikit-learn在Python中生成测试数据集
- OpenStack Neutron之持续测试
- 干货丨 用 Python 进行股票分析
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法