标签:test tin 站点 get 比较 ssi ber else input
Case Number 1: 5
Case Number 2: 0
Case Number 3: impossible
题解:很明显的动态规划,dp[i][j]表示i时刻在j站点还需要的最短等待时间,总共就三种选择,状态转移方程很简单,边界一直是我写动态规划题比较头疼的地方,不过这个题还比较简单,t时刻在n站点自然是0,在其他站点就是INF(为了不会从这些状态转移过去)。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxt = 200 + 10, maxn = 50 + 5; 6 const int INF = 0x3f3f3f3f; 7 8 int read() { 9 int q = 0; char ch = ‘ ‘; 10 while (ch<‘0‘ || ch>‘9‘) ch = getchar(); 11 while (‘0‘ <= ch && ch <= ‘9‘) { 12 q = q * 10 + ch - ‘0‘; 13 ch = getchar(); 14 } 15 return q; 16 } 17 18 int n, t, m1, m2; 19 int ti[maxn]; 20 int dp[maxt][maxn]; 21 bool have_train[maxt][maxn][2]; 22 23 void init() { 24 memset(have_train, false, sizeof(have_train)); 25 for (int i = 1; i <= n - 1; i++) { 26 dp[t][i] = INF; 27 } 28 dp[t][n] = 0; 29 } 30 31 int T = 1; 32 33 int main() 34 { 35 //freopen("input.txt", "r", stdin); 36 while (scanf("%d", &n) && n) { 37 t = read(); 38 init(); 39 for (int i = 1; i < n; i++) { 40 ti[i] = read(); 41 } 42 ti[n] = ti[0] = INF; 43 m1 = read(); 44 int d; 45 for (int i = 0; i < m1; i++) { 46 d = read(); 47 int cnt = 1; 48 for (int j = 1; j <= n; j++) { 49 have_train[d][cnt][0] = true; 50 //printf("d:%d cnt:%d\n", d, cnt); 51 cnt++; 52 d += ti[j]; 53 } 54 } 55 //printf("\n"); 56 57 m2 = read(); 58 for (int i = 0; i < m2; i++) { 59 d = read(); 60 int cnt = n; 61 for (int j = n; j >= 1; j--) { 62 have_train[d][cnt][1] = true; 63 //printf("d:%d cnt:%d\n", d, cnt); 64 cnt--; 65 d += ti[j - 1]; 66 } 67 } 68 69 for (int i = t - 1; i >= 0; i--) { 70 for (int j = 1; j <= n; j++) { 71 dp[i][j] = dp[i + 1][j] + 1; 72 if (i + ti[j] <= t && have_train[i][j][0]) { 73 dp[i][j] = min(dp[i][j], dp[i + ti[j]][j + 1]); 74 } 75 76 if (i + ti[j - 1] <= t && have_train[i][j][1]) { 77 dp[i][j] = min(dp[i][j], dp[i + ti[j - 1]][j - 1]); 78 } 79 } 80 } 81 82 printf("Case Number %d: ", T++); 83 if (dp[0][1] >= INF) { 84 printf("impossible\n"); 85 } 86 else { 87 printf("%d\n", dp[0][1]); 88 } 89 } 90 return 0; 91 }
UVA1025-A Spy in the Metro(动态规划)
标签:test tin 站点 get 比较 ssi ber else input
原文地址:https://www.cnblogs.com/npugen/p/9726796.html