poj 1018Communication System

来源:互联网 发布:音频解码软件 编辑:程序博客网 时间:2024/05/21 15:31
Communication System
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 22559 Accepted: 8037

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
【题意】
有n种装置,每种装置生产一件,每种可由mi个厂家生产,装置有带宽,价格两个属性。把这n种装置都生产出来,B表示这n件装置中最小的带宽,P表示n件装置价格和,求B/P的最小值。
【思路】
枚举最小带宽,每种装置选择带宽>=最小带宽的,且价格最小的。
【代码】
#include<cstdio>#include<algorithm>using namespace std;struct Node{    int bandwidth;    int price;}node[110][110];int band[10010];int lenth[110];int choose[110];bool cmp(Node a,Node b){    return a.bandwidth<b.bandwidth?1:a.bandwidth==b.bandwidth&&a.price<=b.price;}int fin(Node *s,int n,int v)<span style="font-family: 'Times New Roman', Times, serif;">//在node[j]中找出>=band[i]的第一个数.</span>{    int left=0,right=n-1;    int mid;    while(left<=right)    {        mid=(right+left)/2;        if(s[mid].bandwidth<v)            left=mid+1;        else            right=mid-1;    }    if(left>=n)        return 0;    return s[left].price;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        double  ans=0;        int n,p;        scanf("%d",&n);        p=0;        for(int i=0;i<n;i++)        {            scanf("%d",&lenth[i]);            for(int j=0;j<lenth[i];j++)            {                scanf("%d%d",&node[i][j].bandwidth,&node[i][j].price);                band[p++]=node[i][j].bandwidth;            }            sort(node[i],node[i]+lenth[i],cmp);            choose[i]=node[i][lenth[i]-1].price;        }        sort(band,band+p);/*        printf("node\n");        for(int i=0;i<n;i++)        {for(int j=0;j<lenth[i];j++)        {            printf("%d %d   ",node[i][j].bandwidth,node[i][j].price);        }        printf("\n");        }        printf("\n");        */        for(int i=p-1;i>=0;i--)        {            //printf("%d ",band[i]);            if(i!=p-1&&band[i]==band[i+1])                continue;            int sum=0;            int ok=0;            for(int j=0;j<n;j++)            {                int s=fin(node[j],lenth[j],band[i]);                if(s&&s<choose[j])                    choose[j]=s;                if(!s){ok=1;}//此时还不能退出                sum+=choose[j];            }            //printf("sum=%d ",sum);            if(ok)continue;            double temp=(double)band[i]/sum;           // printf("temp=%f  ",temp);            ans=ans>temp?ans:temp;            //printf("ans=%f\n",ans);        }        printf("%.3f\n",ans);    }    return 0;}


0 0
原创粉丝点击