Valley 2583 Metro Spy (uva1025a spy in the Metro)

Source: Internet
Author: User

Valley 2583 Metro Spy (uva1025a spy in the Metro)

Address: http://www.luogu.org/problem/show?pid=2583

Title Description

Agent Maria was sent to S city to perform a particularly dangerous task. She needs to use the subway to do his job, S City's subway only one line running, so it is not complicated.
Maria had a mission, and now the time was 0, and she had to start off from the first station and meet the spies at the last stop. Maria knew that a strong organization was tracking her, and she knew that if she stayed at a station, she would have a great risk of being caught, and it would be safer to hide in a running train. So she decided to stay in the running train as much as she could, and she could only ride forward or back.
Maria in order to arrive on time and safely to the last station to meet each other, you need to know the minimum waiting time at the station sum plan. You have to write a program to get Maria's shortest waiting time. Of course, after the end of the terminal, if the time has not reached the specified moment, she can wait for each other at the station, but this waiting time also to be counted in.
The city has n stations, the number is 1-n, the train is so moving: from the first station to the last station. Or starting at the last stop and meeting. The time of the train between each specific two stations is fixed, and we can ignore the time of the parking, Maria is very fast, so he can get off quickly even if two cars arrive at the same time.

Input/output format

Input Format:

The input file contains more than one set of data, each set of data consists of 7 rows
Line 1th: A positive integer N (2<=n<=50) indicates the number of stations
Line 2nd: A positive integer T (0<=t<=200) indicates the time required to meet
Line 3rd: N (n-1) positive integer (0<ti<70) indicates the train's passage time between two stations
Line 4th: An integer M1 (1<=m1<=50) indicates the number of trains leaving the first station
Line 5th: M1 a positive integer: D1,d2......dn, (0<=d<=250 and Di<di+1) indicates the time of each train leaving its first stop
Line 6th: A positive integer M2 (1<=m2<=50) indicates the number of trains leaving Nth station
Line 7th: M2 A positive integer: E1,E2......EM2, (0<=e<=250 and Ei<ei+1) indicates the time of each train leaving Nth station
The last line has an integer of 0.

output Format:

For each test case, print a single line, "Case number N:" (N starting from 1) and an integer to indicate the shortest time to always wait or a word "impossible" if Maria cannot do it. Follow the output format of the sample.

Input/Output sample

Input Sample # #:

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 # # of output:

Case Number 1:5

Case Number 2:0

Case Number 3:impossible

Description

The first set of examples shows that she got on the bus in 0 minutes, alighted at station 3rd, immediately sat on (0 points from) 15 separate cars go back to station 2nd, immediately sat (20 points originating) 25 drive to the end, 50 points to, also need to wait 5 minutes.

Ideas

DP (Time space).

Time is a natural order =-=, we set d[i][j] means the moment I at the station J need to wait for the minimum time, there is a transfer type:

d[i][j]=min{D[i][j+1]+1,d[i+t[j]][j+1],d[i+t[j-1]][j-1]}

According to the train structure of the input hasedge[i][j][k] indicates moment I is located at Station J there is no train to go to K.

Code

1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 Const intMAXN = -+Ten;7 Const intinf=1e9;8 9 intD[MAXN][MAXN],T[MAXN];Ten BOOLhasedge[maxn][maxn][2]; One intn,m1,m2,t; A  - intdpintIintj) - { the     int& ans=D[i][j]; -     if(ANS)returnans; -     if(I==t && j==n)return 0; -     if((i>t) | | (i==t&&j!=n))returnINF; +      -ans=INF; +Ans=min (ANS,DP (i+1, j) +1); A     if(hasedge[i][j][0] && j<n) ans=min (ANS,DP (i+t[j],j+1)); at     if(hasedge[i][j][1] && j>1) Ans=min (ANS,DP (i+t[j-1],j-1)); -     returnans; - } -  - intMain () { -     intKase=0; in      while(SCANF ("%d", &n) = =1&&N) { -memset (D,0,sizeof(d)); tomemset (Hasedge,0,sizeof(Hasedge)); +          -scanf"%d",&T); the          for(intI=1; i<n;i++) scanf ("%d",&t[i]); *scanf"%d",&M1); $         intSt;Panax Notoginseng          for(intI=1; i<=m1;i++) { -scanf"%d",&St); the             if(st>t)Continue; +              for(intj=1; j<=n;j++) { Ahasedge[st][j][0]=1; theSt + +T[j]; +             } -         } $scanf"%d",&m2); $          for(intI=1; i<=m2;i++) { -scanf"%d",&St); -             if(st>t)Continue; the              for(intj=n;j;j--) { -hasedge[st][j][1]=1;WuyiSt + + t[j-1]; the             } -         } Wu         intANS=DP (0,1); -printf"Case Number %d:",++Kase); About         if(Ans==inf) printf ("impossible\n");Elseprintf"%d\n", ans); $     } -     return 0; -}

Valley 2583 Metro Spy (uva1025a spy in the Metro)

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.