程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1018,poj1018

POJ 1018,poj1018

編輯:C++入門知識

POJ 1018,poj1018


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. 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     大致題意:

  該通信系統需要n種設備,而每種設備分別可以有m1、m2、m3、...、mn個廠家提供生產。

同種設備都會存在兩個方面的差別:bandwidths 和 prices。

現在每種設備都各需要1個,考慮到性價比問題,要求所挑選出來的n件設備,要使得B/P最大。

其中B為這n件設備的帶寬的最小值,P為這n件設備的總價。


解題思路:
  找出最大bandwidths和最小bandwidths,然後從小到大依次枚舉,找出每種bandwidths的最大B/P(即最小prices),再比較出最大的即為answer;
 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 #define N 105
 5 int main()
 6 {
 7     int a[N][N],b[N][N],tot[N];//bandwidths、prices 、輸入計數 
 8     int t,n;
 9     scanf("%d",&t);
10     while(t--)
11     {
12         double ans=0;    
13         int MAX=0,MIN=0xffff;//最大最小 bandwidths
14         scanf("%d",&n);
15         for(int i=1;i<=n;i++)
16         {
17             scanf("%d",&tot[i]);
18             for(int j=1;j<=tot[i];j++)
19             {
20                 scanf("%d%d",&a[i][j],&b[i][j]);
21                 MAX=max(MAX,a[i][j]);
22                 MIN=min(MIN,a[i][j]);
23             }
24         }
25         for(int key=MIN;key<=MAX;key++)//枚舉 
26         {
27             int sum=0;
28             for(int i=1;i<=n;i++)
29             {
30                 int M=0xffff;
31                 for(int j=1;j<=tot[i];j++)
32                     if(a[i][j]>=key&&M>b[i][j]) 
33                         M=b[i][j];
34                 sum+=M;
35             }
36         if(1.0*key/sum>ans) ans=1.0*key/sum;
37         }
38         printf("%.3lf\n",ans);
39     } return 0;
40 }

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved