【算法竞赛入门经典】DAG上的动态规划 例题9-1 UVa1025

【算法竞赛入门经典】DAG上的动态规划 例题9-1 UVa1025

  • 【算法竞赛入门经典】DAG上的动态规划 例题9-1 UVa1025
    • 例题UVa1025
    • 分析
    • 样例实现代码
    • 结果

例题UVa1025

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.
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: 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.

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

分析

时间是单向流动的,当前的时间不可能倒退,天然有序。因此,若以d[i][j]表示第i分钟在第j个车站时,最少需要等待的时间,则d[i][j]组成的是一个DAG(有向无环图)。根据题意,第T分钟必须在第N站会面,因此dp[T][N]=0,即边界条件;而dp[0][1]则为起始点,也是答案所在的位置。
在每个决策点,有三种可能的决策
1.原地等待一分钟:则dp[t][n]=dp[t+1][n]+1
2.如果有往右的列车,搭乘列车:则dp[t][n]=dp[t+tim[n]][n+1]
3.如果有往左的列车,搭乘列车:则dp[t][n]=dp[t+tim[n-1]][n-1]
以上即为状态转移方程

d[i][j]组成的DAG用邻接表储存

样例实现代码

#include
#include
#include
#define NMAX 50+5
#define TMAX 200+5
#define INF 1000000000
using namespace std;
bool hastrain[TMAX][NMAX][2];//0往右,1往左
int dp[TMAX][NMAX];
int tim[NMAX];
int main(){
    int T,N,M1,M2,d,e,temp,kase=0;
    while(cin>>N&&N){
        cin>>T;
        for(int i=1;icin>>tim[i];
        }
        memset(hastrain,0,sizeof(hastrain));
        cin>>M1;
        for(int i=0;icin>>d;
            for(int j=1;jif(d<=T)
                    hastrain[d][j][0]=true;
                d+=tim[j];
            }
        }
        cin>>M2;
        for(int i=0;icin>>e;
            for(int j=N;j>1;j--){
                if(e<=T)
                    hastrain[e][j][1]=true;
                e+=tim[j-1];
            }
        }
        for(int i=1;i<=N;i++){
            dp[T][i]=INF;
        }
        dp[T][N]=0;
        for(int t=T-1;t>=0;t--){
            for(int n=1;n<=N;n++){
                dp[t][n]=dp[t+1][n]+1;
                if(n0]==true&&t+tim[n]<=T){
                    dp[t][n]=min(dp[t][n],dp[t+tim[n]][n+1]);
                }
                if(n>1&&hastrain[t][n][1]==true&&t+tim[n-1]<=T){
                    dp[t][n]=min(dp[t][n],dp[t+tim[n-1]][n-1]);
                }
            }
        }
        cout<<"Case Number "<<++kase<<": ";
        if(dp[0][1]>=INF)
            cout<< "impossible"<else
            cout<0][1]<return 0;
} 

结果

这里写图片描述

你可能感兴趣的:(【书籍】算法竞赛与入门经典)