#include <bits/stdc++.h>
using namespace std;
const int INF = 1<<30;
const int maxn = 50+5;
const int maxt = 200+5;
int N,T,a[maxn],t[maxn],M1,d,M2,e,dp[maxt][maxn],kase;
bool has_train[maxt][maxn][2];
void solve(){
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] << endl;
}
int main(){
while(cin>>N && N){
memset(has_train,0,sizeof(has_train));
cin >> T;
for(int i=1; i<=N-1; i++)
cin >> t[i];
cin >> M1;
for(int i=0; i<M1; i++){
cin >> d;
for(int j=1; j<=N-1; j++){
if(d <= T)
has_train[d][j][0] = 1;
d += t[j];
}
}
cin >> M2;
for(int i=0; i<M2; i++){
cin >> e;
for(int j=N-1; j>=1; j--){
if(e <= T)
has_train[e][j+1][1] = 1;
e += t[j];
}
}
solve();
}
}
dp uva1025
最新推荐文章于 2020-07-08 17:09:25 发布