https://odzkskevi.qnssl.com/f42a2762ecb9e7541fde59742e2f52e7?v=1507521953
【题解】
DP。
dp[i][j]表示在i时间第j个车站最短等待时间
三种决策:
原地等1s
搭乘从左开往右的
搭乘从右开往左的
ok[i][j][0]表示第i车站时间j是否有从左开往右的
ok[i][j][1]表示第i车站时间j是否有从右开往左的
尼玛各种过不去,改的快跟std一样了还是不对,
最终把while(scanf("%d %d", &n, &T) != EOF && n && T)
改成 while(scanf("%d", &n) != EOF && n){read(T);}过了
我*****
浪费我两个多小时
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cmath> 7 #define max(a, b) ((a) > (b) ? (a) : (b)) 8 #define min(a, b) ((a) < (b) ? (a) : (b)) 9 inline void swap(int &a, int &b) 10 { 11 int tmp = a;a = b;b = tmp; 12 } 13 inline void read(int &x) 14 { 15 x = 0;char ch = getchar(), c = ch; 16 while(ch < '0' || ch > '9')c = ch, ch = getchar(); 17 while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar(); 18 if(c == '-')x = -x; 19 } 20 21 const int MAXN = 3000 + 5; 22 const int MAXT = 3000 + 10; 23 24 int n,T,t[MAXN],m1,m2,ok[MAXN][MAXT][2];//0从左,1从右 25 int dp[MAXN][MAXT]; 26 27 int tt; 28 29 int main() 30 { 31 while(scanf("%", &n)!=EOF && n) 32 { 33 read(T); 34 memset(ok, 0, sizeof(ok)); 35 memset(t, 0, sizeof(t)); 36 m1 = m2 = 0; 37 ++ tt; 38 for(register int i = 1;i < n;++ i) 39 read(t[i]); 40 read(m1); 41 for(register int i = 1;i <= m1;++ i) 42 { 43 int tmp;read(tmp); 44 for(register int j = 1;j <= n && tmp <= T;++ j) 45 { 46 ok[j][tmp][0] = 1; 47 tmp += t[j]; 48 } 49 } 50 read(m2); 51 for(register int i = 1;i <= m2;++ i) 52 { 53 int tmp;read(tmp); 54 for(register int j = n;j >= 1 && tmp <= T;-- j) 55 { 56 ok[j][tmp][1] = 1; 57 tmp += t[j - 1]; 58 } 59 } 60 memset(dp, 127, sizeof(dp)); 61 dp[1][0] = 0; 62 for(register int i = 1;i <= T;++ i) 63 for(register int j = 1;j <= n;++ j) 64 { 65 dp[j][i] = dp[j][i - 1] + 1; 66 if(ok[j][i][0]) 67 { 68 dp[j][i] = min(dp[j][i], dp[j - 1][i - t[j - 1]]); 69 } 70 if(ok[j][i][1]) 71 { 72 dp[j][i] = min(dp[j][i], dp[j + 1][i - t[j]]); 73 } 74 } 75 printf("Case Number %d: ", tt); 76 if(dp[n][T] >= 2139062143)printf("impossible "); 77 else printf("%d ", dp[n][T]); 78 } 79 return 0; 80 }