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.
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 }
复制代码

 

目录
相关文章
HDU-1690,Bus System(弗洛伊德)
HDU-1690,Bus System(弗洛伊德)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
78 0
Communication error with the external tax system VERTEX
Communication error with the external tax system VERTEX
100 0
Communication error with the external tax system VERTEX
|
网络虚拟化 5G 安全
|
算法 NoSQL 搜索推荐
Recommender System
Data Sciece from Scratch 之 Recommender System 有一段时间没有看这本书了,今天正好空闲时间将在线视频关于推荐系统学习一下,正好也将这本书关于推荐系统做一个笔记,希望对大家有用。
1026 0
POJ-1004-Finanical Management
Description Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough.
866 0
|
Web App开发 算法