POJ 1018 Communication System (动态规划)

Communication System

Time Limit: 1000MS   Memory Limit: 10000K
Total 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 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

题目大意:

有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;
}

POJ 1018 Communication System (动态规划)

时间: 2024-12-21 02:27:16

POJ 1018 Communication System (动态规划)的相关文章

POJ 1018 Communication System 题解

本题一看似乎是递归回溯剪枝的方法,我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了很久,无奈忍不住偷看了下提示,发现方法真多,有贪心,DP,有高级剪枝的,还有三分法的,八仙过海各显神通啊. 坏习惯了,没思考够深入就偷看提示了. 幸好及时回头,还不需要看别人的代码了.自己做出来之后,有空看看多种解法的代码也好. 然后我想出自己的思路了,使用贪心,剪枝,DP综合优化下,呵呵,最后程序有点复杂,优化到了16ms,运气好点,或者vector换成原始数组的话,应该可以0MS了. 总体思

poj 1018 Communication System (枚举)

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted: 7953 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.

POJ 1018 Communication System(DP)

http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产的同种设备都会存在两个方面的差别:带宽bandwidths 和 价格prices. 现在每种设备都各需要1个,考虑到性价比问题,要求所挑选出来的n件设备,要使得B/P最大. 其中B为这n件设备的带宽的最小值,P为这n件设备的总价. 思路:DP解决. d[i][j]代表选择第i个设备时最小带宽j时的价

贪心/poj 1018 Communication System

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int INF=0xfffffff; 6 struct node 7 { 8 int b,p; 9 }; 10 node a[110][110]; 11 int m[110]; 12 int pmin,psum,bmax,n; 13 double ans; 14 int main() 15

POJ 1018 Communication System

题意:有n种设备,每种设备都有若干制造商,不同制造商提供设备的带宽和价格不同,现需要每种设备各一个,总带宽为这n个设备的最小带宽,总价格为这n个设备的价格之和,求最大的总带宽/总价格. 解法:枚举+剪枝.枚举最小带宽,将所有设备进行排序,排序的优先级为b->p->id,两个剪枝:1.重复的b不需要枚举.2.当前的b无法满足让所有种类的设备都有设备的带宽大于等于b的时候不需要继续枚举. 剪枝效果拔群……觉得是因为数据有点弱?总之32ms就过了……看到网上题解有说dp的……想了一下……没说b的范围

POJ - 1018 Communication System (暴力)

题目大意:要买n个零件,每个零件可以由m个厂家提供,每个零件都有相应的b值和p值.(每个零件只能买一个)现在要求你求出最大的min(b) /sum(p) min(b)表示的是每个零件的最小b值 sum(p)表示的是每个零件的p值的和 解题思路:直接暴力 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define maxn 110 struct device{ in

POJ 1018

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25744   Accepted: 9184 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.

POJ 1018(dp Or 枚举)

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23738   Accepted: 8437 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices.

2017ecjtu-summer training #11 POJ 1018

Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29218   Accepted: 10408 Description We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices