Communication System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20582 | Accepted: 7315 |
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.
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
题意:有n个设备,每个设备都有不同的带宽,每个带宽对应一个费用,从每个设备中选择一个带宽和相应的费用。定义B:全部设备中最小的带宽值;P:全部设备的费用总和。要使选出的设备满足B/P最大。
思路;DP,定义dp[i][j]为取到第i件设备,最小带宽为j时的最小费用,b[i][k]表示第i个设备的第k个带宽值,p[i][k]表示第i个设备的第k个带宽值对应的费用。
转移方程有;当j<b[i][j]时,dp[i][j]=min(dp[i][j],dp[i-1][j]+p[i][k]); 更新以小于b[i][j]的值为最小带宽的最小费用
当j>=b[i][j]时,dp[i][b[i][k]]=min(dp[i][b[i][k]],dp[i-1][j]+p[i][k]); 更新以b[i][j]为最小带宽的最小费用
AC代码:
#include <iostream> #include <cstring> #include <string> #include <cstdio> #include <algorithm> #include <queue> #include <cmath> #include <vector> #include <cstdlib> using namespace std; const double INF=100000000; int n,m[105]; int b[105][105],p[105][105]; int dp[105][1005]; //dp[i][j]表示取到第i件设备,最小带宽为j时的最小费用 void init() { for(int i=1;i<=n;i++) for(int j=1;j<1005;j++) dp[i][j]=INF; for(int k=1;k<=m[1];k++) dp[1][b[1][k]]=p[1][k]; } double solve() { init(); for(int i=2;i<=n;i++) //枚举每个设备 for(int k=1;k<=m[i];k++) //枚举不同的带宽及费用 { for(int j=1;j<b[i][k];j++) //枚举以小于b[i][j]的值为最小带宽的最小费用 { if(dp[i-1][j]!=INF) dp[i][j]=min(dp[i][j],dp[i-1][j]+p[i][k]); //更新以小于b[i][j]的值为最小带宽的最小费用 } for(int j=b[i][k];j<1005;j++) { if(dp[i-1][j]!=INF) dp[i][b[i][k]]=min(dp[i][b[i][k]],dp[i-1][j]+p[i][k]); //更新以b[i][j]为最小带宽的最小费用 } } double ans=0.0; for(int j=0;j<1005;j++) { if(dp[n][j]!=INF) ans=max(ans,j*1.0/dp[n][j]); } return ans; } int main() { int t; cin>>t; while(t--) { cin>>n; for(int i=1;i<=n;i++) { cin>>m[i]; for(int k=1;k<=m[i];k++) cin>>b[i][k]>>p[i][k]; } printf("%.3lf\n",solve()); } return 0; }