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 最短路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; const int MAX_N = ;
const int edge = ;
const int INF = ( << );
struct node {
int d,u;
bool operator < (const node &rhs) const {
return d > rhs.d;
}
}; int N,S,E,M,K;
int first[MAX_N],v[edge],Next[edge];
int w[edge],d1[MAX_N],d2[MAX_N];
int x[MAX_N * ],y[MAX_N * ],w1[MAX_N * ];
bool done[MAX_N];
int p1[MAX_N],p2[MAX_N];
int ou[MAX_N],tem[MAX_N]; void add_edge(int id,int u) {
int e = first[u];
Next[id] = e;
first[u] = id;
} void dijkstra(int s,int *d,int *p) {
fill(p + ,p + N + ,-);
for(int i = ; i <= N; ++i) d[i] = INF;
d[s] = ;
memset(done,,sizeof(done));
priority_queue<node > q;
q.push(node {d[s],s}); while(!q.empty()) {
node x = q.top(); q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = ;
for(int e = first[u]; e != -; e = Next[e]) {
if(d[ v[e] ] > d[u] + w[e]) {
d[ v[e] ] = d[u] + w[e];
q.push(node { d[ v[e] ],v[e]});
p[ v[e] ] = u; }
} }
} void output(int id) {
int len = ;
int len1 = ; if(id != ) {
int a,b;
a = id < ? y[-id] :x[id];
b = id < ? x[-id] :y[id];
for(int p = a; p != -; p = p1[p]) {
tem[len++] = p;
}
for(int i = len - ; i >= ; --i) {
ou[len1++] = tem[i];
}
//printf("b= %d\n",p2[b]);
for(int p = b; p != -; p = p2[p]) {
ou[len1++] = p;
}
} else { for(int p = E; p != -; p = p1[p]) {
ou[len1++] = p;
}
for(int i = ,j = len1 - ; i < j; ++i,--j) {
swap(ou[i],ou[j]);
}
} for(int i = ; i < len1; ++i) {
printf("%d%c",ou[i],i == len1 - ? '\n' : ' ');
}
} void solve() {
dijkstra(S,d1,p1);
dijkstra(E,d2,p2);
int ans = d1[E],id = ;
//printf("ans = %d\n",ans);
for(int i = ; i <= K; ++i) {
if(ans > d1[ x[i]] + d2[ y[i] ] + w1[i]) {
ans = d1[ x[i] ] + d2[ y[i] ] + w1[i];
id = i;
}
if(ans > d1[ y[i] ] + d2[ x[i] ] + w1[i]) {
ans = d1[ y[i] ] + d2[ x[i] ] + w1[i];
id = -i;
}
} output(id);
if(id == ) {
printf("Ticket Not Used\n");
} else {
printf("%d\n",id < ? y[-id] : x[id]);
} printf("%d\n",ans); } int main()
{
//freopen("sw.in","r",stdin);
bool ok = ;
while(~scanf("%d%d%d",&N,&S,&E)) {
if(ok) printf("\n");
ok = ;
scanf("%d",&M);
for(int i = ; i <= N; ++i) first[i] = -;
for(int i = ; i < * M; i += ) {
int u;
scanf("%d%d%d",&u,&v[i],&w[i]);
v[i + ] = u;
w[i + ] = w[i];
add_edge(i,u);
add_edge(i + ,v[i]);
} scanf("%d",&K);
for(int i = ; i <= K; ++i) {
scanf("%d%d%d",&x[i],&y[i],&w1[i]);
} solve(); } //cout << "Hello world!" << endl;
return ;
}