UVA 1025 A Spy in the Metro "Dag dp//three-dimensional tagged array + two-dimensional state array"

Source: Internet
Author: User

Secret Agent Maria was sent to algorithms city to carry out an especially dangerous mission. Afterseveral Thrilling events we find her at the first station of Algorithms city Metro, examining the timetable. The algorithms city Metro consists of a, a, and trains running both ways, so its timetable are not complicated. Maria has a appointment and a local spy at the last station of Algorithms city Metro. Mariaknows that a powerful organization was after her. She also knows and waiting at a station, she isat great risk of being caught. To hide in a running train are much safer, so she decides-stay in Runningtrains as much as possible, even if this means Traveling backward and forward. Maria needs to Knowa schedule and minimal waiting time at the stations that's the last station in time for Hera Ppointment. You must write a program this finds the total waiting time in a best schedule for maria.the Algorithms City Metro system H As N stations, consecutively numbered From 1 to N. Trainsmove in both Directions:from, the last station and from the Thefirst Station. The time required for a train to travel between and consecutive stations is fixed since alltrains move at the same speed. Trains make a very short stop at the which you can ignorefor simplicity. Since She's a very fast agent, Maria can always change trains at a station even if thetrains involved stop on that Statio N at the same time. Inputthe input file contains several test cases. Each test case consists of seven lines with information asfollows. 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 Trainsbetween, consecutive Statio  NS:T1 represents the travel time between the first and stations, t2the time between the second and the third station, andSo on. Line 4. The integer M1 (1≤m1≤50), representing the number of trains departing from the Firststation. Line 5. M1 integers:d1, D2, ..., dM1 (0≤di≤250 and Di < di+1), representing the times at Whichtrains depart from the fi RST station. Line 6. The integer M2 (1≤m2≤50), representing the number of trains departing from the N-thstation.line 7. M2 integers:e1, E2, ..., eM2 (0≤ei≤250 and Ei < ei+1) representing the times at Whichtrains depart from the N-t H Station. The last case was followed by a line containing a single zero. Outputfor each test case, print a line containing the case number (starting with 1) and an integer representingthe total w Aiting time in the stations for a best schedule, or the word ' impossible ' in case Maria isunable to make the appointment. Use the format of the sample output. Sample Input4555 1540 5 2040 5 154181 2 3 6 1260 3 5 7 152302012071 3 5 7 170Sample outputcase Nu Mber 1:5case number 2:0case number 3:imPossible 
#include <bits/stdc++.h>using namespace Std;const int INF = 1e6;int n,m,t;int t[60];int d[260][60];int ok[260][60][    2];int Main () {int kase=0;        while (cin>>n,n) {cin>>t;        for (int i=1;i<n;i++) cin>>t[i];        int M1;        cin>>m1;        memset (ok,0,sizeof (OK));            for (int i=1;i<=m1;i++) {int tm,j=1;            cin>>tm;                while (tm<=t && j<n) {ok[tm][j][0]=1;            Tm+=t[j++];        }} int M2;        cin>>m2;            for (int i=1;i<=m2;i++) {int tm,j=n;            cin>>tm;                while (tm<=t && j>1) {ok[tm][j][1]=1;            TM+=T[--J];        }} for (int i=1;i<n;i++) D[t][i]=inf;        d[t][n]=0; for (int i=t-1, i>=0; i--) {for (int j=1; j<=n; J + +) {D[i][j] = d[i+1][J] + 1;                if (j<n && ok[i][j][0] && i+t[j]<=t) d[i][j]=min (d[i][j],d[i+t[j]][j+1]); if (j>1 && ok[i][j][1] && i+t[j-1]<=t) d[i][j]=min (d[i][j],d[i+t[j-1]][j-1            ]);        }} printf ("Case Number%d:", ++kase);        if (d[0][1] > INF) printf ("impossible\n");    else printf ("%d\n", d[0][1]); }}

UVA 1025 A Spy in the Metro "Dag dp//three-dimensional tagged array + two-dimensional state array"

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.