Communication System
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
题目类型:线性DP
算法分析:dp[i][j]表示选择前i种服务带宽为j的最小费用,初始化dp[i][j] = INF,状态转移方程为dp[i][min(b[i][j],k)] = min(dp[i][min(b[i][j],k)], dp[i-1][k] + p[i][j]),最后枚举更新i / dp[n][i]的最大值即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
/************************************************** filename :f.cpp author :maksyuki created time :2017/2/14 21:00:49 last modified :2017/2/14 21:21:49 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CPPFF ifstream cin ("in") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 100 + 2; int b[maxn][maxn], p[maxn][maxn], dp[maxn][maxn*maxn]; int main() { //CFF; //CPPFF; int t; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); int maxb = -INF; for(int i = 1; i <= n; i++) { int mi; scanf("%d", &mi); for(int j = 1; j <= mi; j++) { scanf("%d%d", &b[i][j], &p[i][j]); maxb = max(maxb, b[i][j]); } b[i][0] = mi; } for(int i = 0; i < maxn; i++) for(int j = 0; j < maxn * maxn; j++) dp[i][j] = INF; for(int i = 1; i <= b[1][0]; i++) dp[1][b[1][i]] = min(dp[1][b[1][i]], p[1][i]); for(int i = 2; i <= n; i++) for(int j = 1; j <= b[i][0]; j++) for(int k = 0; k <= maxb; k++) if(dp[i-1][k] < INF) dp[i][min(b[i][j],k)] = min(dp[i][min(b[i][j],k)], dp[i-1][k] + p[i][j]); double res = -INF; for(int i = 0; i <= maxb; i++) if(dp[n][i] < INF) res = max(res, i / 1.0 / dp[n][i]); printf("%.3lf\n", res); } return 0; } |
- « 上一篇:poj2686
- poj1179:下一篇 »