Uva1025 - A Spy in the Metro

 Secret agent Maria was sent to Algorithms City to carry out an especiallydangerous mission. After several thrillingevents we find her in the first station of Algorithms City Metro, examining thetime table. The Algorithms CityMetro 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 powerfulorganization is after her. She also knows that while waiting at a station,she is at great risk of being caught. To hidein a running train is much safer, so she decides to stay in running trainsas much as possible, even if this meanstraveling backward and forward. Maria needs to know a schedule with minimalwaiting time at the stations that getsher to the last station in time for her appointment. You must write a programthat finds the total waiting time in abest schedule for Maria.

The Algorithms City Metro system has N stations, consecutively numbered from 1 toN.Trains move in bothdirections: from the first station to the last station and from the last stationback to the first station. The timerequired for a train to travel between two consecutive stations is fixed sinceall 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 thatstation at the same time.

\epsfbox{p2728.eps}

Input 

The input file contains several test cases. Each test case consists of seven lines withinformation as follows.
Line 1.
The integer N ( 2$ \le$N$ \le$50), which is the number of stations.
Line 2.
The integer T ( 0$ \le$T$ \le$200), which is the time of the appointment.
Line 3.
N - 1 integers: t1,t2,...,tN - 1 ( 1$ \le$ti$ \le$70),representing the travel times for the trains between twoconsecutive stations: t1 represents the travel time between the first two stations, t2 the time between the second and the third station, and so on.
Line 4.
The integer M1 ( 1$ \le$M1$ \le$50), representing the number of trainsdeparting from the first station.
Line 5.
M1 integers: d1,d2,...,dM1 ( 0$ \le$di$ \le$250and di < di + 1), representing the times at which trains depart from the first station.
Line 6.
The integer M2 ( 1$ \le$M2$ \le$50), representing the number of trainsdeparting from the N-th station.
Line 7.
M2 integers: e1,e2,...,eM2 ( 0$ \le$ei$ \le$250and ei < ei + 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 aninteger representing the totalwaiting time in the stations for a best schedule, or the word ` impossible' in caseMaria is unable to make theappointment. Use the format of the sample output.

Sample Input 

4
55
5 10 15
4
0 5 10 20
4
0 5 10 15
4
18
1 2 3
5
0 3 6 10 12
6
0 3 5 7 12 15
2
30
20
1
20
7
1 3 5 7 11 13 17
0

Sample Output 

Case Number 1: 5
Case Number 2: 0
Case Number 3: impossible

状态:dp(i,j)——在时刻i,你在车站j的最少等待时间。
转移:

1    等1分钟
2    搭乘向右开的车
3    搭乘向左开的车

#include <iostream>
#include <cstdio>
#include <cstring>
#define min(a,b) (a>b?b:a)
const int  maxn = 250 + 10 ;
const int inf = 1<<30 ;
using namespace std;
bool has_train[maxn][maxn][2] ;  // has_train[t][i][0] 时刻t , 车站i , 往右边是否有车  ;
                                 // has_train[t][i][0] 时刻t , 车站i , 往左边是否有车  ; 
int dp[maxn][maxn] , t[maxn] , lt[maxn] , rt[maxn] ;
                                // dp[i][j] 时刻i , 车站j  ;  t[i] 从车站i 到 i+1 的时间 
                                // lt[] , rt[] , 分别代表从1出发的车的时间 , 从n 出发的车的时间
int main() {
    int  n , T  , text = 1 ;
    while( ~scanf("%d",&n ),n) {
            scanf("%d",&T) ;
        for( int i = 1 ; i < n ; ++i ){
            scanf("%d",&t[i] );
        }
        int lenum , rinum ;             // 左边车数, 右边车数
        int  sum ;                      
        scanf("%d",&lenum);
        memset( has_train, 0 , sizeof( has_train)) ;
        for (int i = 1 ; i <= lenum ; ++i ) {
            scanf("%d",<[i] ) ;
            sum = lt[i] ;                   
            if( lt[i] <= T ) { has_train[sum][1][0] = true ;
            for( int j = 1  ; j < n ; ++j ) {
                sum += t[j] ;               //记录时间
                if( sum <= T ) {
                has_train[sum][j+1][0] = true ;  // 注意j+1
                }
            }
        }
    }
        scanf("%d",&rinum);
        for ( int i =1 ;  i<= rinum ; ++i ){
            scanf("%d",&rt[i] ) ;
            sum = rt[i] ;
            if( rt[i] <= T ) {
                has_train[sum][n][1] = true ;
            for ( int j = n -1; j > 0 ; --j ) {
                    sum += t[j] ;
                if( sum <= T) {
                    has_train[sum][j][1] = true ; // 注意j
                }
            }


        }
    }
        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 ;            // 策略1 , 等待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] ) ;// 向左
                    }
            }
        }
          printf("Case Number %d: ",text++) ;
        if( dp[0][1] >= inf ) {
            printf("impossible\n");
        }
        else {
          printf("%d\n",dp[0][1]) ;
        }


    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值