pku1018 Communication System
http://poj.org/problem?id=1018
DP,写的好蒙。。。
1 #include <stdio.h> 2 #include <map> 3 4 using namespace std; 5 6 #define N 110 7 8 int b[N][N], p[N][N]; 9 int dp[N][N*N]; 10 const int maxint = 1<<30; 11 12 int main() 13 { 14 int i, j, t, n, m, x, y, k; 15 float max1, temp1; 16 map<int, int> map1, map2; 17 map<int, int>::iterator it; 18 scanf("%d", &t); 19 while(t-- && scanf("%d", &n)) 20 { 21 map1.clear(); 22 map2.clear(); 23 k = 1; 24 for(i=1; i<=n; i++) 25 { 26 scanf("%d", &m); 27 b[i][0] = m; 28 for(j=1; j<=m; j++) 29 { 30 scanf("%d%d", &x, &y); 31 if(map1.find(x) == map1.end()) 32 { 33 map1.insert(make_pair(x, k)); 34 k ++; 35 } 36 b[i][j] = x; 37 p[i][j] = y; 38 } 39 } 40 for(i=0; i<N*N; i++) 41 { 42 dp[0][i] = dp[1][i] = maxint; 43 } 44 for(j=1; j<=b[1][0]; j++) 45 { 46 dp[0] [map1[b[1][j]]] = min(dp[0] [map1[b[1][j]]], p[1][j]); 47 } 48 for(i=2; i<=n; i++) 49 { 50 for(j=1; j<=b[i][0]; j++) 51 { 52 for(it=map1.begin(); it!=map1.end(); it++) 53 { 54 if(dp[0][(*it).second]==maxint) 55 { 56 continue; 57 } 58 if((*it).first <= b[i][j]) 59 { 60 dp[1][map1[(*it).first]] = min(dp[1][map1[(*it).first]], dp[0][map1[(*it).first]]+p[i][j]); 61 } 62 else 63 { 64 dp[1][map1[b[i][j]]] = min(dp[1][map1[b[i][j]]], dp[0][map1[(*it).first]]+p[i][j]); 65 } 66 } 67 } 68 for(j=0; j<N*N; j++) 69 { 70 dp[0][j] = dp[1][j]; 71 dp[1][j] = maxint; 72 } 73 } 74 for(it=map1.begin(); it!=map1.end(); it++) 75 { 76 map2.insert(make_pair((*it).second, (*it).first)); 77 } 78 max1 = -1; 79 for(j=0; j<N*N; j++) 80 { 81 if(dp[0][j]==maxint) 82 { 83 continue; 84 } 85 temp1 = (float)map2[j] / (float)dp[0][j]; 86 if(temp1 > max1) 87 { 88 max1 = temp1; 89 } 90 } 91 printf("%.3f\n", max1); 92 } 93 return 0; 94 }