uva1025 A Spy in the Metro

来源:互联网 发布:pyro fireshooter淘宝 编辑:程序博客网 时间:2024/05/23 17:41

Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro consists of a single line with trains running both ways, so its time table is not complicated.

Maria has an appointment with a local spy at the last station of Algorithms City Metro. Maria knows that a powerful organization is after her. She also knows that while waiting at a station, she is at great risk of being caught. To hide in a running train is much safer, so she decides to stay in running trains as much as possible, even if this means traveling backward and forward. Maria needs to know a schedule with minimal waiting time at the stations that gets her to the last station in time for her appointment. You must write a program that finds the total waiting time in a best schedule for Maria.

The Algorithms City Metro system has N stations, consecutively numbered from 1 to N . Trains move in both directions: from the first station to the last station and from the last station back to the first station. The time required for a train to travel between two consecutive stations is fixed since all trains move at the same speed. Trains make a very short stop at each station, which you can ignore for simplicity. Since she is a very fast agent, Maria can always change trains at a station even if the trains involved stop in that station at the same time.

Input

The input file contains several test cases. Each test case consists of seven lines with information as follows.
Line 1.
The integer N ( 2 N 50 ), which is the number of stations.
Line 2.
The integer T ( 0 T 200 ), which is the time of the appointment.
Line 3.
N - 1 integers: t 1 , t 2 ,..., t N - 1 ( 1 t i 70 ), representing the travel times for the trains between two consecutive stations: t 1 represents the travel time between the first two stations, t 2 the time between the second and the third station, and so on.
Line 4.
The integer M 1 ( 1 M 150 ), representing the number of trains departing from the first station.
Line 5.
M 1 integers: d 1 , d 2 ,..., d M1 ( 0 d i 250 and d i < d i + 1 ), representing the times at which trains depart from the first station.
Line 6.
The integer M 2 ( 1 M 250 ), representing the number of trains departing from the N -th station.
Line 7.
M 2 integers: e 1 , e 2 ,..., e M2 ( 0 e i 250 and e i < e i + 1 ) representing the times at which trains depart from the N -th station.

The last case is followed by a line containing a single zero.

Output

For each test case, print a line containing the case number (starting with 1) and an integer representing the total waiting time in the stations for a best schedule, or the word ` impossible ' in case Maria is unable to make the appointment. Use the format of the sample output.

时间是单向流动的,是一个天然的序。影响到决策的只有当前时间和所处的车站用dp[i][j]表示在时刻i,车站j,最少还需等待多长时间。因此边界条件就是dp[T][n]=0;(在目标时刻,在目标车站时无需等待)。其他dp为正无穷。主要有三种决策

决策1:等待一分钟

决策2:搭乘向左开的车(如果有的话)

决策3:搭乘向右开的车(如果有的话)

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<stack>#include<queue>#include<map>#include<set>#include<algorithm>using namespace std;int main(){    int n;    int T;    int i,j,k;    int t[55];    int kase=0;    int m1,m2;    int a,b,coun;    bool hastrain[305][55][2];    int dp[205][55];    while(~scanf("%d",&n)&&n!=0){        memset(hastrain,false,sizeof(hastrain));        memset(t,0,sizeof(t));        memset(dp,0,sizeof(dp));        kase++;        scanf("%d",&T);        for(i=1;i<n;i++)            scanf("%d",&t[i]);        scanf("%d",&m1);        for(i=0;i<m1;i++){            scanf("%d",&a);            coun=a;            hastrain[coun][1][0]=true;            for(j=1;j<n;j++){                coun+=t[j];                hastrain[coun][j+1][0]=true;            }        }        scanf("%d",&m2);        for(i=0;i<m2;i++){            scanf("%d",&a);            coun=a;            hastrain[coun][n][1]=true;            for(j=n-1;j>=1;j--){                coun+=t[j];                hastrain[coun][j][1]=true;            }        }        for(i=1;i<n;i++)            dp[T][i]=0x3f3f3f3f;        dp[T][n]=0;        for(i=T-1;i>=0;i--){            for(j=1;j<=n;j++){                dp[i][j]=dp[i+1][j]+1;                if(j<n&&hastrain[i][j][0]&&(i+t[j]<=T))                    dp[i][j]=min(dp[i][j],dp[i+t[j]][j+1]);                if(j>1&&hastrain[i][j][1]&&(i+t[j-1]<=T))                    dp[i][j]=min(dp[i][j],dp[i+t[j-1]][j-1]);            }        }        printf("Case Number %d: ",kase);        if(dp[0][1]>=0x3f3f3f3f)            printf("impossible\n");        else            printf("%d\n",dp[0][1]);    }    return 0;}


0 0
原创粉丝点击