# Algorithmic Competition Primer-examples and after-school training (dynamic programming)

Source: Internet
Author: User

1. Subway Spy (Reverse derivation time method)

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 and Output Sample input example # #:
`4555 10 1540 5 10 2040 5 10 154181 2 350 3 6 10 1260 3 5 7 12 152302012071 3 5 7 11 13 170`
Sample # # of output:
`Case number 1:5case number 2:0case 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.

`/*the inverse method of time: The shortest time from the next moment to T to find the shortest time of the current moment, pay attention to the preprocessing in this topic, solve the DP process need to determine whether can ride the train problem (other topics should be compared to each other)*/#include<iostream>using namespacestd; #include<cstdio>#defineN 501#defineT 2001#defineINF 1<<30#include<cstring>intN,t,tim[n],m1,trainm1[n],m2,trainm2[n];inthave_train[t][n][2];intKase=0;intF[t][n];voidinput () {memset (Tim,0,sizeof(Tim)); memset (TRAINM1,0,sizeof(TRAINM1)); memset (TRAINM2,0,sizeof(trainm2)); memset (Have_train,0,sizeof(Have_train)); Memset (F,0,sizeof(f)); scanf ("%d",&t);  for(intI=1; i<=n-1;++i) {scanf ("%d",&Tim[i]); } scanf ("%d",&M1);  for(intI=1; i<=m1;++i) {scanf ("%d",&Trainm1[i]); intk=1;  for(intj=trainm1[i];k<=n;j+=tim[k-1]) {have_train[j][k][0]=1;/*have_train[][][] Record J moment, the K station, 0 means the right of the subway, 1 means the left of the subway, which is preprocessing, DP when easy to write equations*/k++; }} scanf ("%d",&m2);  for(intI=1; i<=m2;++i)/*the same preprocessing*/{scanf ("%d",&Trainm2[i]); intk=N;  for(intj=trainm2[i];k>=1; j+=Tim[k]) {have_train[j][k][1]=1; K--; }            }    }voidDP () { for(intI=1; i<=n-1;++i) f[t][i]=inf;/*The boundary condition of the equation of motion, at the moment of T, the minimum waiting time to reach N station is 0*/F[t][n]=0;  for(inti=t-1; i>=0;--i) for(intj=1; j<=n;++j) {F[i][j]=f[i+1][j]+1;/*This means that Maria did not go, but waited in situ for a minute, noting that it was time to launch the present in the coming time*/          if(j<n&&have_train[i][j][0]&AMP;&AMP;I+TIM[J]&LT;=T)/*recursion: At I moment J Station has the right subway, then ride to the next stop, time from the next stop deduction this station*/F[i][j]=min (f[i][j],f[i+tim[j]][j+1]); if(j>1&&have_train[i][j][1]&&i+tim[j-1]<=t) f[i][j]=min (f[i][j],f[i+tim[j-1]][j-1]); }}intMain () { for(;;) {scanf ("%d",&N); if(n==0)         Break; ++Kase; printf ("Case Number %d:", Kase);        Input ();        DP (); if(f[0][1]>=inf)/*if the launch of the No. 0 moment, the first point to the end of the shortest waiting time >inf, that is, no state, no way to travel, then output impossible, it is worth noting that the INF here is also equivalent to a flag*/printf ("Impossible"); Elseprintf"%d", f[0][1]); printf ("\ n"); }    return 0;}`
View Code

Algorithmic Competition Primer-examples and after-school training (dynamic programming)

Related Keywords:

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.