时间是单向流逝的,是一个天然的序,每一个时间有三个选择:
1.等一分钟
2.搭上向左开的车(如果有)
3.搭上向右开的车(如果有)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
const int INF=0x3f3f3f3f;
LL mod=1e9+7;
const int N=1005;
int n,T,t[55],startl[55],startr[55];
int dp[205][55]; //dp[i][j]表示在i时刻到达j站等待最少时间
int has_train[205][55][2];//在i时刻到达j站是否有车
void init(int x,int y)
{
for(int i=1; i<=x; i++)
{
int temp=startl[i];
has_train[temp][1][0]=1;
for(int j=2; j<=n; j++)
{
temp+= t[j-1];
has_train[temp][j][0]=1;
}
}
for(int i=1; i<=y; i++)
{
int temp=startr[i];
has_train[temp][n][1]=1;
for(int j=n-1; j>=1; j--)
{
temp+= t[j];
has_train[temp][j][1]=1;
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int kase=0;
while(cin>>n,n)
{
cin>>T;
memset(has_train,0,sizeof(has_train));
for(int i=1; i<n; i++)
{
cin>>t[i];
}
int x,y;
cin>>x;
for(int i=1; i<=x; i++)
cin>>startl[i];
cin>>y;
for(int i=1; i<=y; i++)
cin>>startr[i];
//
init(x,y);
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"<<endl;
else
cout<<dp[0][1]<<endl;
}
}