1. 程式人生 > >uva 1025 A Spy int the Metro

uva 1025 A Spy int the Metro

size namespace targe else %d tar cnblogs += metro

https://vjudge.net/problem/UVA-1025

看見spy忍俊不禁的想起省賽時不知道spy啥意思 ( >_<

f[i][j]表示i時刻處於j站所需的最少等待時間,有三種可能,一是i-1時刻就在這裏然後等待了1時刻 f[i][j]=f[i-1][j]+1 ; 二是正好由由左邊相鄰的一個車站開過來(如果可以的話) f[i][j]=f[i-t[j-1]][j-1]; 三是正好由右邊的車站開過來(if can) f[i][j]=f[i-t[j]][j+1]; 取三者的最小值就好,最後如果f[T][N]>=inf表示impossible.

 1 #include<bits/stdc++.h>
 2
using namespace std; 3 #define inf 0x3f3f3f3f 4 int t[55],tl[55],tr[55]; 5 int f[255][55]; 6 bool can[255][55][2]; 7 int main() 8 { 9 int N,T,M1,M2,i,j,k=0; 10 while(cin>>N&&N){ 11 cin>>T; 12 for(i=1;i<N;++i) scanf("%d",t+i); 13 memset(f,inf,sizeof
(f)); 14 memset(can,0,sizeof(can)); 15 cin>>M1; 16 for(i=1;i<=M1;++i) 17 { 18 int s; 19 scanf("%d",tl+i); 20 for(j=1,s=tl[i];j<=N&&s<=T;s+=t[j++]) 21 { 22 can[s][j][0]=1; 23 }
24 } 25 cin>>M2; 26 for(i=1;i<=M2;++i) 27 { 28 int s; 29 scanf("%d",tr+i); 30 for(j=N,s=tr[i];j>=1&&s<=T;s+=t[j-1],j--) 31 can[s][j][1]=1; 32 } 33 f[0][1]=0; 34 for(i=1;i<=T;++i) 35 { 36 for(j=1;j<=N;++j) 37 { 38 f[i][j]=f[i-1][j]+1; 39 if(j>1&&i-t[j-1]>=0&&can[i-t[j-1]][j-1][0]) 40 f[i][j]=min(f[i][j],f[i-t[j-1]][j-1]); 41 if(j<N&&i-t[j]>=0&&can[i-t[j]][j+1][1]) 42 f[i][j]=min(f[i][j],f[i-t[j]][j+1]); 43 } 44 } 45 printf("Case Number %d: ",++k); 46 if(f[T][N]>=inf) puts("impossible"); 47 else cout<<f[T][N]<<endl; 48 } 49 return 0; 50 } 51 /* 52 4 53 55 54 5 10 15 55 4 56 0 5 10 20 57 4 58 0 5 10 15 59 60 ans=5 61 */

uva 1025 A Spy int the Metro