Communication System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28273 | Accepted: 10074 |
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
一道醉人题。。。输出时用printf("%.3f",res); 第一次做wa以为思路出现了偏差。。。然后换了个思路做了一遍。。。两次都卡了
好久。。。还有就是没有数据规模。。。
自己的思路:
#include<iostream> #include<cstdio> #include<cstring> #include<stdlib.h> using namespace std; #define N 105 struct Dev { int width,price; } dev[N][N]; Dev dp1[N][N]; double dp2[N][N]; int main() { int t; scanf("%d",&t); while(t--) { int n,num[105]; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&num[i]); for(int j=1; j<=num[i]; j++) scanf("%d%d",&dev[i][j].width,&dev[i][j].price); } for(int i=1; i<=num[1]; i++) { dp1[1][i].price=dev[1][i].price; dp1[1][i].width=dev[1][i].width; dp2[1][i]=dp1[1][i].width/(double)dp1[1][i].price; } for(int i=2; i<=n; i++) for(int j=1; j<=num[i]; j++) { dp2[i][j]=0; for(int k=1; k<=num[i-1]; k++) { int width=min(dp1[i-1][k].width,dev[i][j].width); int price=dp1[i-1][k].price+dev[i][j].price; if(width/(double)price-dp2[i][j]>1e-6) { dp2[i][j]=width/(double)price; dp1[i][j].width=width; dp1[i][j].price=price; } else if(abs(width/(double)price-dp2[i][j])<1e-6) { if(width>dp1[i][j].width) { dp1[i][j].width=width; dp1[i][j].price=price; } } } } double res=0; for(int i=1;i<=num[n];i++) if(dp2[n][i]>res) res=dp2[n][i]; printf("%.3f ",res); } return 0; }
题解思路:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define N 105 #define INF 999999999 struct Dev { int width,price; } dev[N][N]; int dp[N][1205]; int main() { int t; scanf("%d",&t); while(t--) { memset(dp,0,sizeof(dp)); int n,num[105]; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&num[i]); for(int j=1; j<=num[i]; j++) scanf("%d%d",&dev[i][j].width,&dev[i][j].price); } for(int i=1;i<=n;i++) for(int j=1;j<=1200;j++) dp[i][j]=INF; for(int i=1; i<=num[1]; i++) dp[1][dev[1][i].width]=min(dev[1][i].price,dp[1][dev[1][i].width]); for(int i=2; i<=n; i++) for(int j=1; j<=num[i]; j++) { for(int k=0; k<=1200; k++) { if(dp[i-1][k]==INF) continue; if(k<=dev[i][j].width) dp[i][k]=min(dp[i-1][k]+dev[i][j].price,dp[i][k]); else dp[i][dev[i][j].width]=min(dp[i-1][k]+dev[i][j].price,dp[i][dev[i][j].width]); } } double res=0; for(int i=1; i<=1200; i++) { if(dp[n][i]==INF) continue; //cout<<i/(double)dp[n][i]<<endl; if(i/(double)dp[n][i]>res) res=i/(double)dp[n][i]; } printf("%.3f ",res); } return 0; }