UVa 1025 A Spy in the Metro dp : DAG、最短路

UVA - 1025
A Spy in the Metro
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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 timetable. The Algorithms City Metro consists of a single line with trains running both ways, so its timetable is not complicated.

  Maria has an appointment with a local spy at the last station of Algorithms City Metro. Mariaknows that a powerful organization is after her. She also knows that while waiting at a station, she isat great risk of being caught. To hide in a running train is much safer, so she decides to stay in runningtrains 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. Trainsmove in both directions: from the first station to the last station and from the last station back to thefirst station. The time required for a train to travel between two consecutive stations is fixed since alltrains move at the same speed. Trains make a very short stop at each station, which you can ignorefor simplicity. Since she is a very fast agent, Maria can always change trains at a station even if thetrains 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: t1, t2, . . . , tN−1 (1 ≤ ti ≤ 20), representing the travel times for the trains between two consecutive 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 ≤ M1 ≤ 50), representing the number of trains departing from the first station.

  Line 5. M1 integers: d1, d2, . . . , dM1 (0 ≤ di ≤ 250 and di < di+1), representing the times at which trains depart from the first station.

  Line 6. The integer M2 (1 ≤ M2 ≤ 50), representing the number of trains departing from the N-th station.

  Line 7. M2 integers: e1, e2, . . . , eM2 (0 ≤ ei ≤ 250 and 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 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.


The question is from here.


My Solution:


要么往右走,要么往左走,要么等1个单位的时间;

总共 O(nT);


注意点:1、时间变化是 t[ j ] 不是 1;

       2、那里少个if 不仅浪费时间而且导致数组越界访问;

       3、输出格式 Presentation error 在 : 后面有一个 空格也不要漏掉,注意看仔细Sample Output;


#include <iostream>
#include <cstdio>
#include <cstring>
//#define LOCAL
using namespace std;
const long long INF=0x3f3f3f3f;
int t[72],dp[205][52],has_train[206][52][3];

void init()
{
    memset(t,0,sizeof(t));
    memset(dp,0,sizeof(dp));
    memset(has_train,0,sizeof(has_train));
}

int main()
{
    #ifdef LOCAL
    freopen("a.txt","r",stdin);
    #endif // LOCAL
    int n,T,kase=0;
    while(1){
        init();
        scanf("%d",&n);
        if(n==0) break;
        scanf("%d",&T);

        for(int i=1;i<n;i++) scanf("%d",&t[i]);


        int M,d;  //M1,M2
        scanf("%d",&M);
        for(int i=1;i<=M;i++){
            scanf("%d",&d);
            for(int j=1;j<=n;j++){
                 has_train[d][j][0]=1;    //has_train[d++][j][1]=1;不对的
                 d+=t[j];
                 if(d>T) break; 	  //前面没有加这个if RE了,因为那样不仅浪费时间计算了不需要计算的东西,
            }				  //而 d 大于 T 时会出现数组访问越界 

        }
        scanf("%d",&M);
        for(int i=1;i<=M;i++){
            scanf("%d",&d);
            for(int j=n;j>=1;j--){
                has_train[d][j][1]=1;    //has_train[d++][j][1]=1;不对的
                d+=t[j-1];
                if(d>T) break;
            }

        }


        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]); //向左走
            }

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

    }

    return 0;
}

谢谢


你可能感兴趣的:(dp,ACM,最短路,uva,DAG)