Ultraviolet A-11374 Airport Express (Dijkstra template + enumeration)

Source: Internet
Author: User
Description

Problem D: Airport Express

In a small city called iokh, a train service,Airport-Express, Takes residents to the airport more quickly than other transports. There are two types of trains in airport-Express,Economy-xpressAndCommercial-xpress. They travel at different speeds, take different routes and have different costs.

Jason is going to the airport to meet his friend. he wants to take the specified cial-xpress which is supposed to be faster, but he doesn't have enough money. luckily he has a ticket for the specified cial-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 is not easy for him.

Jason now seeks your help. the routes of the two types of trains are given. please write a program to find the best route to the destination. the program shoshould also tell when the ticket shoshould be used.

Input

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

The first line of each case contains 3 integers, namelyN,SAndE(2 ≤N≤ 500, 1 ≤S,EN), Which represent the number of stations, the starting point and where the airport is located respectively.

There is an integerM(1 ≤M≤ 1000) representing the number of connections between the stations of the economy-xpress. The nextMLines give the information of the routes of the economy-xpress. Each consists of three IntegersX,YAndZ(X,YN, 1 ≤Z≤ 100). This meansXAndYAre connected and it takesZMinutes to travel between these two stations.

The next line is another integerK(1 ≤K≤ 1000) representing the number of connections between the stations of the specified cial-xpress. The nextKLines contain the information of the specified cial-xpress in the same format as that of the economy-xpress.

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

Output

For each case, You shocould first list the number of stations which Jason wowould 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 shoshould get on the train of specified cial-xpress. finally, print the total time for the journey on the last line. consecutive sets of output must be separated by a blank line.

Sample Input
4 1 441 2 21 3 32 4 43 4 512 4 3
Sample output
1 2 425

There are two ways to go to the airport. One is the economic line and the other is the commercial line. The routes, speeds, and prices are different. You have a commercial ticket and you can take a commercial line, in other cases, you can only make economic lines. The transfer time is not counted. Your task is to find the fastest route to the airport.

Idea: enumerate the start and end of the business line, and then we can find the optimal solution from the start point and the end point.

# Include <iostream> # include <cstdio> # include <vector> # include <cstring> # include <algorithm> # include <queue> using namespace STD; const int maxn = 505; const int INF = 0x3f3f3f; struct edge {int from, to, DIST;}; struct heapnode {int D, U; bool operator <(const heapnode RHs) const {return D> RHS. d ;}}; struct Dijkstra {int n, m; // points and number of edges vector <edge> edges; // edge list vector <int> G [maxn]; // The edge number starting from each vertex (starting from 0) bool Done [maxn]; // whether int d [maxn] has been marked; // The distance from int P [maxn] to each point; // the top point in the shortest path, it can also be the last side void Init (int n) {This-> N = N; For (INT I = 0; I <n; I ++) g [I]. clear (); edges. clear ();} void addedge (int from, int to, int Dist) {edges. push_back (edge) {from, to, DIST}); M = edges. size (); G [from]. push_back m-1);} void Dijkstra (INT s) {priority_queue 






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.