POJ 1018 Communication System (动态规划)

来源:互联网 发布:神玥软件招聘 编辑:程序博客网 时间:2024/05/29 10:53

Communication System
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 22500 Accepted: 8008

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 33 100 25 150 35 80 252 120 80 155 402 100 100 120 110

Sample Output

0.649

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest

题目大意:

有T组测试数据,每组1个n,表示n行,接下来n行,每行一个m,表示有m个管道,每个管道有流量和费用,最后求从n行中,每行选择1个管道,要求 B/P最大 ,B表示所选的那个方案中n个管道的最小的那个的流量,P表示n个管道费用和。

解题思路:

朴素的动态规划,dp[i]记录,当前B为i的P,也就是流量的最小费用和。实现可以利用队列代替滚动数组。

解题代码:

#include <iostream>#include <cstdio>#include <queue>#include <iomanip>using namespace std;const int maxn=1100;const int inf=0x3f3f3f3f;int dp[maxn];struct node{    int flow,sum;    node(int flow0=0,int sum0=0){        flow=flow0,sum=sum0;    }};void solve(){    queue <node> q;    q.push(node(inf,0));    int n,m,flow,price;    scanf("%d",&n);    for(int i=1;i<=n;i++){        scanf("%d",&m);        for(int i=0;i<maxn;i++) dp[i]=inf;        while(m-- >0){            scanf("%d%d",&flow,&price);            int qsize=q.size();            while(qsize-- >0){                node s=q.front();                q.pop();                if(s.flow<flow){                    if(s.sum+price<dp[s.flow]) dp[s.flow]=s.sum+price;                }else{                    if(s.sum+price<dp[flow]) dp[flow]=s.sum+price;                }                q.push(s);            }        }        while(!q.empty()) q.pop();        for(int i=0;i<maxn;i++){            if(dp[i]<inf) q.push(node(i,dp[i]));        }    }    double ans=0;    while(!q.empty()){        node s=q.front();        q.pop();        if(double(s.flow)/double(s.sum) > ans) ans= double(s.flow)/double(s.sum) ;    }    cout<<setiosflags(ios::fixed)<<setprecision(3)<<ans<<endl;}int main(){    int t;    scanf("%d",&t);    while(t-- >0){        solve();    }    return 0;}



4 0
原创粉丝点击