UVA 11374 Airport Express (Shortest way)

Source: Internet
Author: User

UVA 11374 Airport Express

In a small city called Iokh, a train service, Airport-express, takes residents to the Airport more quickly than other Tran Sports. There is types of trains in Airport-express, the economy-xpress and the commercial-xpress. They travel at different speeds, take different routes and has different costs.

Jason is going to the airport to meet his friend. He wants to take the commercial-xpress which was supposed to being faster, but he doesn ' t has enough money. Luckily he has a ticket for the commercial-xpress which can take him one station forward. If He used the ticket wisely, he might end up saving a lot of time. However, choosing the best time to use the ticket are not easy for him.

Jason now seeks your help. The routes of the types of trains is given. Please write a program to find the best route to the destination. The program should also the ticket should is used.

Input

The input consists of several test cases. Consecutive cases is separated by a blank line.

The first line of all case contains 3 integers, namely N, S and E (2≤n≤500, 1≤s, e≤n), which represent the number of stations, the starting point and where the airport is located respectively.

There is an integer M (1≤m≤1000) representing the number of connections between the stations of the economy-xpress. The next M lines give the information of the routes of the economy-xpress. Each consists of three integers x, Y and Z (X, Y≤n, 1≤z≤100). This means X and Y is connected and it takes Z minutes to travel between these-stations.

The next line is another integer K (1≤k≤1000) representing the number of connections between the stations of the Comme Rcial-xpress. The next K lines contain the information of the commercial-xpress in the same format as that of the economy-xpress.

All connections is bi-directional. You may assume this there is exactly one optimal route to the airport. There might is cases where you must use your ticket on order to reach the airport.

Output

For each case, you should first list the number of stations which Jason would visit in order. On the next line, output ' Ticket not used ' If you decided ' not ' to use the Ticket; Otherwise, state the station where Jason should get on the train of Commercial-xpress. Finally, print the total time for the journey in the last line. Consecutive sets of output must is separated by a blank line.

Sample Input

4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3

Sample Output

1 2 4
2
5

There are two kinds of tickets, business tickets and economic tickets. For economic reasons, the business ticket can only buy one, the economic ticket to buy more than a card. Tickets are both two-way. Now ask the shortest path from the starting point to the end point, as well as the total time at which the business ticket is used at the most cost-effective and final cost. Calculate two shortest circuits without using a business ticket, calculate the minimum path of the starting point s to each point ds[n], and the shortest path from the end T to each point Dt[n], and record the shortest path min from the starting point to the end point. Then enumerate the business tickets (Business ticket initial site U, terminate site V, cost COS), make min = min (Ds[u] + dt[v] + cos, ds[v] + dt[u] + cos), and record the use of the business ticket of U and v. The min after maintenance is the shortest cost of using the business ticket in the best case, then the pre array, as well as the recorded U and v output paths.
#include <cstdio>#include <cstring>#include <queue>using namespace STD;Const intN =505;Const intM =5005;Const intINF =0x3f3f3f3f;typedef Long LongllintN, S, t;intVis[n], d[2][n], en;intHEAD[M];intpre[2][n];structNode {intTo, DIS, next; }edge[n * M];voidInit ();voidAddedge (intUintVintX) {edge[en].to = v;      Edge[en].next = Head[u];      Edge[en].dis = x;      Head[u] = en++;      edge[en].to = u;      Edge[en].next = Head[v];      Edge[en].dis = x;  HEAD[V] = en++; }voidSPFA (intFlag) { Queue<int>Q; for(inti =1; I <= N;          i++) {D[flag][i] = INF; Vis[i] =0; Pre[flag][i] =-1; } D[flag][s] =0; Vis[s] =1;      Pre[flag][s] = s; Q.push (s); while(! Q.empty ()) {intU = Q.front (); Vis[u] =0; Q.pop (); for(inti = Head[u]; I! =-1; i = edge[i].next) {intv = edge[i].to;if(D[flag][u] + Edge[i].dis < d[flag][v])                  {D[flag][v] = D[flag][u] + Edge[i].dis; PRE[FLAG][V] = u;if(!vis[v])                      {Q.push (v); VIS[V] =1; }              }          }      }  }voidInput () {intNum, A, b, C;scanf("%d", &num); for(inti =0; i < num; i++) {scanf(" %d%d%d", &a, &b, &c);    Addedge (A, B, c); }}voidPrintintx) {if(pre[0][X] = = x) {printf("%d", x);return; } Print (pre[0][X]);printf("%d", x);}voidSolve () {intNum, A, b, C;intMin = d[0][s], TU =-1, TV =-1;scanf("%d", &num); for(inti =0; i < num; i++) {scanf(" %d%d%d", &a, &b, &c);if(d[0][a] + d[1][B] + C < min) {min = d[0][a] + d[1][B] + C;        TU = A, TV = b; }if(d[0][B] + d[1][a] + C < min) {min = d[0][B] + d[1][a] + C;        TU = B, TV = A; }    }if(TU = =-1) {print (s);puts("");printf("Ticket not used\n"); }Else{print (TU); for(inti = TV; I! = s; i = pre[1][i])printf("%d", i);printf("%d\n", s);printf("%d\n", TU); }printf("%d\n", Min);}intMain () {intCase =1; while(scanf(" %d%d%d", &n, &s, &t) = =3) {memset(Head,-1,sizeof(head));if(Case! =1)puts("");        case++;                    Input (); SPFA (0);        s = t; SPFA (1);        Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 11374 Airport Express (Shortest way)

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.