Example-09-01

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>
#include <cstring>

int min(int a, int b)
{
    if (a < b) return a;
    else return b;
}

int main()
{
    int N; // 2 <= N <= 50
    int t[55];
    int T; // 0 <= T <= 200
    int M1, M2; // 1 <= M1, M2 <= 50
    int d[55], e[55];

    int have_train[205][55][2]; // [time][station][direction]
                                // time <= 200
                                // sation <= 50
                                // direction == 0 left, == 1 right

    int kase = 1;

    while (scanf("%d", &N) && N) {
        scanf("%d", &T);
        for (int i = 1; i < N; i++)
            scanf("%d", &t[i]); // t[i] is the time between station i and station i+1

        memset(have_train, 0, sizeof(have_train));

        scanf("%d", &M1);

        for (int i = 0; i < M1; i++){ // for each train
            int start;
            scanf("%d", &start);
            have_train[start][1][0] = 1; // station 1
            for (int j = 1; j < N; j++) { // station 2, 3, 4..., N; j+1 is the station number
                // start + t[j] is the time the train arrives at station j+1, also is start time + time from station 1 to station j+1
                // t[j] is the time between station j and station j+1 
                if (start + t[j] <= T) {
                    have_train[start + t[j]][j + 1][0] = 1;
                }
                start += t[j];
            }
        }
        
        scanf("%d", &M2);
        for (int i = 0; i < M2; i++){ // for each train
            int start;
            scanf("%d", &start);
            have_train[start][N][1] = 1; // station N
            for (int j = N-1; j >= 1; j--) { // station N-1, N-2, ..., 1; j is the station number
                // start + t[j] is the time the train arrives at station j, also is start time + time from station N to station j
                // t[j] is the time between station j and station j+1 
                if (start + t[j] <= T) {
                    have_train[start + t[j]][j][1] = 1;
                }
                start += t[j];
            }
        }

        #define INF 10000000
        int counts = 0;
        int dp[205][205];

        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 && i + t[j] <= T && have_train[i][j][0]){
                    dp[i][j] = min(dp[i][j], dp[i+t[j]][j+1]); // t[j] is the time between station j and station j+1 
                }
                if (j > 1 && i + t[j-1] <= T && have_train[i][j][1]){
                    dp[i][j] = min(dp[i][j], dp[i+t[j-1]][j-1]); // t[j-1] is the time between station j-1 and station j
                }
            }
        }

        if (dp[0][1] >= INF)
            printf("Case Number %d: impossible\n", ++counts);
        else
            printf("Case Number %d: %d\n", ++counts, dp[0][1]);

    }

    return 0;
}

 

posted @ 2016-08-12 11:03  PatrickZhou  阅读(123)  评论(0编辑  收藏  举报