• POJ1018:Communication System(枚举+搜索剪枝) java程序员


    Communication System
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 19377   Accepted: 6851

    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
    MYCode:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define MAX 110
    struct node
    {
        int b;
        int p;
    };
    node st[MAX][MAX];
    int num[MAX];
    int list[10010];
    int n;
    bool mark;
    bool cmp(node a,node b)
    {
        if(a.p!=b.p)
        {
            return a.p<b.p;
        }
        else
        {
            return a.b<b.b;
        }
    }
    int solve(int lim)
    {
        bool flag,sign=true;
        int i,j;
        int sum=0;
        for(i=1;i<=n;i++)
        {
            bool flag=false;
            for(j=1;j<=num[i];j++)
            {
                if(st[i][j].b>=lim)
                {
                    flag=true;
                    sum+=st[i][j].p;
                    break;
                }
            }
            if(flag==false)
            {
                sign=false;
                break;
            }
        }
        if(sign==false)
        {
            mark=false;
            return -1;
        }
        else
        return sum;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int i,j;
            int ct=0;
            scanf("%d",&n);
            for(i=1;i<=n;i++)
            {
                scanf("%d",&num[i]);
                for(j=1;j<=num[i];j++)
                {
                    scanf("%d%d",&st[i][j].b,&st[i][j].p);
                    list[ct++]=st[i][j].b;
                }
                sort(st[i]+1,st[i]+num[i]+1,cmp);
            }
            sort(list,list+ct);
            double ans=-1;
            mark=true;
            for(i=0;i<ct;i++)
            {
                if(mark==false)
                break;
                if(i==0||list[i]!=list[i-1])
                {
                    int res=solve(list[i]);
                    if(list[i]*1.0/res*1.0>ans)
                    ans=(list[i]*1.0)/res;
                }
            }
            printf("%.3lf\n",ans);
        }
    }
    //16MS
    枚举最小的B,复杂度是1E8,加上剪枝,16MS,有点夸张,本来以为会TLE.
    是因为数据不够强吗?
  • 相关阅读:
    构造函数作为友元函数的参数
    引用调用
    分块查找
    折半查找
    c++中map按key和value排序
    STL之map学习实例
    STL之stack
    STL之map
    STL之string
    STL之template类模板
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215881.html
Copyright © 2020-2023  润新知