#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<deque>
#include<functional>
#include<iterator>
#include<vector>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#define CPY(A, B) memcpy(A, B, sizeof(A))
typedef long long LL;
typedef unsigned long long uLL;
const int MOD = int (1e9) + 7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos (-1.0);
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
int dp[210][55];
int has_train[210][55][2];
int t[210],d1[55],d2[55];
int main() {
int n,T,m1,m2; int kase=1;
while (~scanf ("%d",&n) &&n) {
cin>>T;
for (int i=1; i<n; ++i) { cin>>t[i]; }
cin>>m1;
for (int i=1; i<=m1; ++i) { cin>>d1[i]; }
cin>>m2;
for (int i=1; i<=m2; ++i) { cin>>d2[i]; }
memset (has_train,0,sizeof (has_train) );
for (int i=1; i<=m1; ++i) {
has_train[d1[i]] [1][0]=1;//ith train when go from station 1
int temp=d1[i];
for (int j=1; j<n; j++) {
temp+=t[j];//station one by one
if (temp<=T) { has_train[temp][j+1][0]=1; }//when arrive station j
else { break; }
}
}
for (int i=1; i<=m2; i++) {
has_train[d2[i]] [n][1]=1;
int temp=d2[i];
for (int j=n-1; j>1; j--) {
temp+=t[j];
if (temp<=T) { has_train[temp][j][1]=1; }
else { break; }
}
}
for (int i=1; i<=n-1; i++) { dp[T][i]=INF; }
dp[T][n]=0;
for (int i=T-1; i>=0; i--)
for (int j=1; j<=n; j++) {
dp[i][j]=dp[i+1][j]+1;
if (j<n&&has_train[i][j][0]&&i+t[j]<=T) {
dp[i][j]=min (dp[i][j],dp[i+t[j]][j+1]);
}
if (j>1&&has_train[i][j][1]&&i+t[j-1]<=T) {
dp[i][j]=min (dp[i][j],dp[i+t[j-1]][j-1]);
}
}
cout<<"Case Number "<<kase++<<": ";
if (dp[0][1]>=INF) { cout<<"impossible\n"; }
else { cout<<dp[0][1]<<"\n"; }
}
return 0;
}
UVa 1025 A Spy in the Metro
最新推荐文章于 2021-05-21 21:43:19 发布