zoukankan      html  css  js  c++  java
  • POJ 1018 Communication System (动态规划)

    版权声明:欢迎关注我的博客,本文为博主【炒饭君】原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/26887425

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



  • 相关阅读:
    Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter]
    HTTP的长连接和短连接
    Nginx(三)nginx 反向代理
    Nginx(四)nginx 负载均衡
    postgresql 数据库 INSERT 或 UPDATE 大量数据时速度慢的原因分析
    低层次父母,喜欢不停地“讲道理”,而高层次父母,会做2件事
    oracle数据库数据量如何计算,怎么查看oracle数据库数据量大小?
    Git的eclipse插件(下载、抓取、提交、恢复、比较)
    Linux 上 定时备份postgresql 数据库的方法
    Nginx(二)nginx.conf 配置文件
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10518531.html
Copyright © 2011-2022 走看看