【HDOJ】1474 Always On the Run
普通DP。基本和floyd一个思路。
1 /* 1474 */ 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 6 #define MAXN 15 7 #define MAXK 1005 8 #define MAXD 35 9 #define INF 999999 10 11 int dp[MAXN][MAXK]; 12 int day[MAXN][MAXD]; 13 int price[MAXN][MAXN][MAXD]; 14 15 int min(int a, int b) { 16 return a<b ? a:b; 17 } 18 19 int main() { 20 int i, j, k, p; 21 int n, m, d; 22 int t = 0; 23 24 #ifndef ONLINE_JUDGE 25 freopen("data.in", "r", stdin); 26 #endif 27 28 while (scanf("%d %d", &n, &m)!=EOF && (n||m)) { 29 for (i=1; i<=n; ++i) { 30 for (j=1; j<=n; ++j) { 31 if (i == j) 32 continue; 33 scanf("%d", &day[i][j]); 34 for (k=1; k<=day[i][j]; ++k) 35 scanf("%d", &price[i][j][k]); 36 } 37 } 38 39 for (i=1; i<=n; ++i) 40 for (j=0; j<=m; ++j) 41 dp[i][j] = INF; 42 43 dp[1][0] = 0; 44 for (k=1; k<=m; ++k) { 45 for (i=1; i<=n; ++i) { 46 for (j=1; j<=n; ++j) { 47 if (i == j) 48 continue; 49 d = (k-1)%day[j][i] + 1; 50 if (price[j][i][d] && dp[j][k-1]!=INF) 51 dp[i][k] = min(dp[i][k], dp[j][k-1]+price[j][i][d]); 52 } 53 } 54 } 55 56 printf("Scenario #%d\n", ++t); 57 if (dp[n][m] == INF) 58 printf("No flight possible.\n\n"); 59 else 60 printf("The best flight costs %d.\n\n", dp[n][m]); 61 } 62 63 return 0; 64 }