Ultraviolet A 11374 Airport Express (Short Circuit), 11374 airport
Ultraviolet A 11374 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, the Economy-Xpress and the specified cial-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, 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 are connected and it takes Z minutes to travel between these two stations.
The next line is another integer K (1 ≤ K ≤1000) representing the number of connections between the stations of the specified cial-Xpress. the next K lines 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 showould 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 shocould get on the train of your 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 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 types of tickets: commercial tickets and financial tickets. For economic reasons, only one business ticket can be bought, and multiple economical tickets can be bought. Both tickets are bidirectional. Now we will ask the shortest path from the start point to the end point, and the site where the business ticket is used for the most cost-effective and the total time spent. If no commercial ticket is used, calculate the two shortest paths, and calculate the shortest paths from the starting point s to each point ds [N]. and the minimum short-circuit dt [N] From the end point t to each point, and the minimum short-circuit Min from the start point to the end point is recorded. Then enumerate the commercial tickets (the initial site u of the commercial tickets, end site v, and Cost cos), so that Min = min (ds [u] + dt [v] + cos, ds [v] + dt [u] + cos), and records the u and v of the used commercial ticket. The Min after maintenance is the minimum short-circuit cost of the commercial ticket under the optimal conditions, and then according to the pre array and the recorded u and v output paths.
#include <cstdio>#include <cstring>#include <queue>using namespace std;const int N = 505;const int M = 5005;const int INF = 0x3f3f3f3f;typedef long long ll;int n, s, t;int vis[N], d[2][N], en;int head[M];int pre[2][N];struct node { int to, dis, next; }edge[N * M]; void init();void addEdge(int u,int v,int x) { 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++; } void SPFA(int flag) { queue<int> Q; for(int i = 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()) { int u = Q.front(); vis[u] = 0; Q.pop(); for(int i = head[u]; i != -1; i = edge[i].next) { int v = 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; } } } } } void input() { int num, a, b, c; scanf("%d", &num); for (int i = 0; i < num; i++) { scanf("%d %d %d", &a, &b, &c); addEdge(a, b, c); }}void print(int x) { if (pre[0][x] == x) { printf("%d", x); return; } print(pre[0][x]); printf(" %d", x);}void solve() { int num, a, b, c; int Min = d[0][s], tU = -1, tV = -1; scanf("%d", &num); for (int i = 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 (int i = tV; i != s; i = pre[1][i]) printf(" %d", i); printf(" %d\n", s); printf("%d\n", tU); } printf("%d\n", Min);}int main() { int Case = 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.