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: t1, t2,..., tN - 1 ( 1
ti
70), representing the travel times for the trains between two consecutive stations: t1represents 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
#include <cstdio> #include <cstring> int inf = 1<<30; // 站台个数 int N; // 约定见面时间 int T; // t[i]代表从站台i到站台i+1的时间 int t[100]; // 从站台1开出的火车个数 int M1; // 从站台1开出火车的出发时间 int d[100]; // 从最后一个站台开出的火车个数 int M2; // 从最后一个站台开出火车的出发时间 int e[100]; // 记录数据 // record[i][j]代表从j时刻在站台i到最终会面时间的最少等待时间 int record[60][210]; // left_time[i]代表从站台1到达站台i的时间 int left_time[60]; // right_time[i]代表从最后一个站台到达站台i的时间 int right_time[60]; int get_min(int x, int this_t); int main() { int count = 1; while(scanf("%d", &N) == 1 && N != 0) { // 读入数据 scanf("%d", &T); left_time[1] = 0; right_time[N] = 0; for(int i = 1; i <= N-1; i++) { scanf("%d", &t[i]); left_time[i+1] = left_time[i] + t[i]; } for(int i = N-1; i >= 1; i--) right_time[i] = right_time[i+1] + t[i]; scanf("%d", &M1); for(int i = 1; i <= M1; i++) scanf("%d", &d[i]); scanf("%d", &M2); for(int i = 1; i <= M2; i++) scanf("%d", &e[i]); // 初始化 memset(record, -1, sizeof(record)); /* printf("left: "); for(int i = 1; i <= N; i++) printf("(%d:%d) ", i, left_time[i]); printf("\nright: "); for(int i = 1; i <= N; i++) printf("(%d:%d) ", i, right_time[i]); printf("\n"); */ // 计算结果 int result = get_min(1, 0); printf("Case Number %d: ", count); count++; if(result == inf) printf("impossible\n"); else printf("%d\n", result); } return 0; } // 计算结果 // 代表计算在站台x,现在时刻为this_t到达最终站台所用最少等待时间 // 如果不可能,返回inf int get_min(int x, int this_t) { // printf("here: %d, time: %d\n", x, this_t); if(record[x][this_t] != -1) return record[x][this_t]; record[x][this_t] = inf; if(x == N) { record[x][this_t] = T-this_t; } // 查看从左到右开的所有火车 for(int i = 1; i <= M1; i++) { if(x == N) break; if(d[i]+left_time[x] >= this_t && d[i]+left_time[x+1] <= T) { // printf("\tnow: %d, time: %d\n", x+1, d[i]+left_time[x+1]); int t_result = get_min(x+1, d[i]+left_time[x+1]); if(t_result != inf) { int t_time = t_result + (d[i]+left_time[x]-this_t); if(t_time < record[x][this_t]) record[x][this_t] = t_time; } } } // 查看从右到左开的所有火车 for(int i = 1; i <= M2; i++) { if(x == 1) break; if(e[i]+right_time[x] >= this_t && e[i]+right_time[x-1] <= T) { // printf("\tnow: %d, time: %d\n", x-1, e[i]+right_time[x-1]); int t_result = get_min(x-1, e[i]+right_time[x-1]); if(t_result != inf) { int t_time = t_result + (e[i]+right_time[x]-this_t); if(t_time < record[x][this_t]) record[x][this_t] = t_time; } } } return record[x][this_t]; }
做的第一道动态规划题目,纪念一下。
首先读完题最好想一想能不能贪心,如果不能的话举出反例,然后再想动态规划。
状态是record(i, j)代表j时刻在站台i到最终时刻所用最少等待时间。
用记忆化搜索完成,不可能的状态值设为(1 << 30).