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, the Economy-Xpress and the Commercial-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 Commercial-Xpress which is supposed to be faster, but he doesn't have 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 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 should also tell when the ticket should 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 Zminutes 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 Commercial-Xpress. The next K lines contain the information of the Commercial-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 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 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
Problemsetter: Raymond Chun
Originally appeared in CXPC, Feb. 2004 --------------------
最后不能留空行= = wtf?
--------------------
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; const int maxn=111111; const int maxm=2222222; const int INF=1e9; 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; vector<Edge> edges; vector<int> G[maxn]; bool done[maxn]; int d[maxn]; int p[maxn]; 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<HeapNode>que; for (int i=0;i<n;i++) d[i]=INF; d[s]=0; memset(done,0,sizeof(done)); que.push((HeapNode){0,s}); while (!que.empty()){ HeapNode x=que.top(); que.pop(); int u=x.u; if (done[u]) continue; done[u]=true; for (int i=0;i<G[u].size();i++){ Edge& e=edges[G[u][i]]; if (d[e.to]>d[u]+e.dist){ d[e.to]=d[u]+e.dist; p[e.to]=G[u][i]; que.push((HeapNode){d[e.to],e.to}); } } } } }solver; int d1[maxn],p1[maxn]; int d2[maxn],p2[maxn]; int n,s,e; void dfs1(int u) { if (u==s) { printf("%d",u+1); return; } Edge e=solver.edges[p1[u]]; dfs1(e.from); printf(" %d",u+1); } void dfs2(int u) { if (u==e) { printf(" %d",u+1); return; } printf(" %d",u+1); Edge e=solver.edges[p2[u]]; dfs2(e.from); } int main() { int m,x,y,z,k; int cnt=0; while (~scanf("%d%d%d",&n,&s,&e)) { if (cnt++) puts(""); s--;e--; solver.init(n); scanf("%d",&m); while (m--) { scanf("%d%d%d",&x,&y,&z); x--; y--; solver.addedge(x,y,z); solver.addedge(y,x,z); } solver.dijkstra(s); for (int i=0;i<n;i++) { d1[i]=solver.d[i]; p1[i]=solver.p[i]; } solver.dijkstra(e); for (int i=0;i<n;i++) { d2[i]=solver.d[i]; p2[i]=solver.p[i]; } int ans=d1[e]; int a=-1,b=-1; scanf("%d",&k); for (int i=0;i<k;i++) { scanf("%d%d%d",&x,&y,&z); x--; y--; if (d1[x]+d2[y]+z<ans) { ans=d1[x]+d2[y]+z; a=x; b=y; } if (d1[y]+d2[x]+z<ans) { ans=d1[y]+d2[x]+z; a=y; b=x; } } if (a==-1) { dfs1(e); puts(""); puts("Ticket Not Used"); printf("%d\n",ans); } else { dfs1(a); //dfs1(de,b,e); dfs2(b); puts(""); printf("%d\n",a+1); printf("%d\n",ans); } } return 0; }