UVa 1025 A Spy in the Metro (Dynamic planning)

Source: Internet
Author: User

Transmission Door

Description

Secret Agent Maria was sent to algorithms city to carry out an especially dangerous mission. After several thrilling events we find she in the first station of the algorithms city Metro, examining the time table. The algorithms city Metro consists of a, a, and trains running both ways, so it time table is not complicated. Maria has a appointment and a local spy at the last station of Algorithms city Metro. Maria knows that a powerful organization was after her. She also knows and waiting at a station, she's at great risk of being caught. To hide in a running train are much safer, so she decides-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's the last station Appointment. You must write a program this 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 last station and the last Stati On back to the first station. The time required for a train to travel between and consecutive stations is fixed since all trains move at the same speed. Trains make a very short stop at the which you can ignore for simplicity. Since She's a very fast agent, Maria can always change trains at a station even if the trains involved stop in that Stati On the same time.

Input

The input file contains several test cases. Each test case consists the 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 both consecutive S TATIONS:T1 represents the travel time between the first and stations, T2 the time between the second and the third Statio n, 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 F Irst 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 was 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 waitin G time in the stations for a best schedule, or the word ' impossible ' in case Maria was unable to make the appointment. Use the format of the sample output.

Sample Input

4
-
5
4
0 5
4
0 5
4
-
1 2 3
5
0 3 6
6
0 3 5 7
2
-
-
1
-
7
1 3 5 7
0

Sample Output

Case Number 1:5

Case Number 2:0

Case Number 3:impossible

Ideas

  Test instructions

A city's subway is linear, there are N (2≤n≤50) stations, from left to right numbered 1~n. There are M1 trains from the first stop to the right, and M2 train from Nth station to the left. At the moment 0,mario from 1th station, the aim is at the moment T (0≤t≤200) to meet the station N of a spy. Waiting in the station is easy to get caught, so she decided to try to hide on the train, so that the total waiting time at the station is as short as possible. Train station parking time is negligible, and Mario is nimble, even if two different directions of the train at the same time on the station, Mario can also complete the transfer. Minimum wait time.

  Ideas:

  Mario has three decisions in a certain state:

      • Wait a minute.
      • Drive to the right (if available)
      • Drive to the left (if available)

Only the current time and the station are affected by the current decision, so you can use D (i,j) to indicate time I, you at Station J, at least how long you need to wait. Boundary condition D (t,n) = 0, other d (T,J) is positive infinity.

/* *dp[i][j] Indicates the moment I, at the station J, at least also need to wait how long *has_train[t][i][0] to indicate the moment T, in the station I have the right to open the train *has_train[t][i][1] show the moment T, at the station I have left open train * /#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int MAXN = 55;const int maxt = 205;const int INF = 0x3f3f3f3f;int T[MAXN],HAS_TRAIN[MAXT][MAXN][2],DP[MAXT][MAXN ];int Main () {int n,case = 0;while (~scanf ("%d", &n) && n) {int t,m1,m2,d;memset (has_train,0,sizeof (has_train ); memset (Dp,0,sizeof (DP)); SCANF ("%d", &t), for (int i = 1;i < n;i++) scanf ("%d", &t[i]); scanf ("%d", &m1); while (m1--) {scanf ("%d", &d), for (int j = 1;j < n;j++) {if (d <= T) has_train[d][j][0] = 1;d + = T[j];}} scanf ("%d" , &m2), while (m2--) {scanf ("%d", &d), for (int j = n-1;j > 0;j--) {if (d <= T) has_train[d][j+1][1] = 1;d + = T[j ];}} for (int i = 1;i < n;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:", ++case);DP [0][1]>=inf? printf ("impossible\n"):p rintf ("%d\n", Dp[0][1]);} return 0;}

  

UVa 1025 A Spy in the Metro (Dynamic planning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.