1025 - A Spy in the Metro

来源:互联网 发布:微软雅黑字体 for mac 编辑:程序博客网 时间:2024/06/07 18:08

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 ( 2N50), which is the number of stations.
Line 2.
The integer T ( 0T200), which is the time of the appointment.
Line 3.
N - 1 integers: t1, t2,…, tN - 1 ( 1ti70), 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 ( 1M150), representing the number of trains departing from the first station.
Line 5.
M1 integers: d1, d2,…, dM1 ( 0di250 and di < di + 1), representing the times at which trains depart from the first station.
Line 6.
The integer M2 ( 1M250), representing the number of trains departing from the N-th station.
Line 7.
M2 integers: e1, e2,…, eM2 ( 0ei250 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

特工玛丽亚被送到算法的城市进行一个特别危险的任务。一些激动人心的事件后,我们发现她在算法的城市地铁的第一站,检查表的时间。算法的城市地铁由一行与列车运行方式,所以它的时间表并不复杂。
玛丽亚已经预约当地的间谍在算法的城市地铁的最后一站。玛丽亚知道后一个强大的组织。她也知道,在车站等待,她被抓的是冒着极大的危险。藏在一个运行的列车更安全,所以她决定留在尽可能运行列车,即使这意味着向后和向前旅行。玛丽亚需要知道一个时间表以最小等待时间在车站,她的最后一站在时间的约会。你必须编写一个程序,找到最佳的总等待时间安排玛丽亚。
算法城市地铁系统N站,连续编号从1到N .火车朝着两个方向:从第一站到最后一站,从最后一站回第一站。火车旅行所需的时间连续两站之间是固定的,因为所有列车以同样的速度移动。火车要很短的停留在每个车站,你可以忽略为简单起见。因为她是一个非常快的代理,玛丽亚可以随时改变列车在一个车站即使火车停止参与站在同一时间。
输入
输入文件包含几个测试用例。每个测试用例由7行信息如下。
1号线。
整数N(2 obasanjo)站的数量。
第2行。
整数T(0 t200一样),这是约会的时间。
第3行。
N - 1整数:t1,t2,……tN - 1(1 ti70),代表着连续两站之间的火车旅行时间:t1代表前两站之间的旅行时间,t2第二和第三站之间的时间,等等。
4号线。
整数M1(1 m150),代表着火车离开第一站的数量。
第5行。
M1整数:d1,d2,…dM1(0 di250和di < di + 1),代表的时代列车离开第一站。
第6行。
整数M2(1 m250),代表开方的火车离开车站。
第7行。
M2整数:e1,e2,…eM2反应堆(0 ei250和ei < ei + 1)代表的时代列车离开第n个站。
最后情况是紧随其后的是一行包含一个零。
输出
对于每个测试用例,打印一行包含箱号(从1开始)和一个整数代表站的总等待时间最好安排,或“ impossible”这个词,以防玛丽亚无法使这一任命。使用样例输出的格式。
样例输入
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
样例输出
案例1:5
案例2:0
案例3号: impossible

#include<iostream>#include<cstring>using namespace std;const int maxn = 50 + 5;const int maxt = 200 + 5;const int INF = 1000000000;// has_train[t][i][0]表示时刻t,在车站i是否有往右开的火车int t[maxn], has_train[maxt][maxn][2];int dp[maxt][maxn];int main() {  int kase = 0, n, T;  while(cin >> n >> T && n) {    int M1, M2, d;    for(int i = 1; i <= n-1; i++) cin >> t[i];    // 预处理,计算has_train数组    memset(has_train, 0, sizeof(has_train));    cin >> M1;    while(M1--) {      cin >> d;      for(int j = 1; j <= n-1; j++) {        if(d <= T) has_train[d][j][0] = 1;        d += t[j];      }    }    cin >> M2;    while(M2--) {      cin >> d;      for(int j = n-1; j >= 1; j--) {        if(d <= T) has_train[d][j+1][1] = 1;        d += t[j];      }    }    // DP主过程    for(int i = 1; i <= n-1; i++) dp[T][i] = INF;    dp[T][n] = 0;    for(int i = T-1; i >= 0; i--)//我们从时刻T-1时枚举,类似时光逆流      for(int j = 1; j <= n; j++) {//dp[i][j]表示在i时刻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]); // 左      }    // 输出    cout << "Case Number " << ++kase << ": ";    if(dp[0][1] >= INF) cout << "impossible\n";    else cout << dp[0][1] << "\n";  }  return 0;}
0 0