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, theEconomy-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 nextM lines give the information of the routes of the Economy-Xpress. Each consists of three integersX, Y and Z (X,Y ≤ N, 1 ≤ Z ≤ 100). This meansX and Y are connected and it takesZ 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 Commercial-Xpress. The nextK 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 "TicketNot 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 thetotal 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
M条经济线路,K条商业线路,从S到E最多选择一条商业线路,最短路是多少。
很无语这个题其实算简单的,但是却写了挺久的。。一直WA,最后终于找到错了。。。
求两遍最短路,一次到起点,一次到终点。f和g数组分别表示到起点和终点的最短路。对K条商业线路找f[u]+dist(u到v商业线路长度)+g[v]最小值就行了,dijkstra用优先队列和vector的话,复杂度是O(MlogN+K)。
主要被坑在打印路径上。。注意前半段根据f倒着递归,后半段根据g正着递归。。
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 510
#define MAXM 2000010
#define MAXNODE 105
#define MOD 100000
#define SIGMA_SIZE 4
typedef long long LL;
using namespace std;
int N,S,E,M,K,f[MAXN],g[MAXN];
struct Edge{
int u,v,dist;
};
struct HeapNode{
int u,d;
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];
void init(int n){
this->n=n;
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}
void add_edge(int u,int v,int dist){
edges.push_back((Edge){u,v,dist});
m=edges.size();
G[u].push_back(m-1);
}
void dijkstra(int s,int* d){
priority_queue<HeapNode> q;
for(int i=0;i<n;i++) d[i]=INF;
d[s]=0;
memset(done,0,sizeof(done));
q.push((HeapNode){s,0});
while(!q.empty()){
HeapNode x=q.top();
q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
int L=G[u].size();
for(int i=0;i<G[u].size();i++){
Edge& e=edges[G[u][i]];
if(d[e.v]>d[u]+e.dist){
d[e.v]=d[u]+e.dist;
q.push((HeapNode){e.v,d[e.v]});
}
}
}
}
}solver;
void print_road1(int u,int e){
if(u==e){
printf("%d",u);
return;
}
else{
int L=solver.G[u].size();
for(int i=0;i<L;i++){
Edge& edge=solver.edges[solver.G[u][i]];
if(f[u]-edge.dist==f[edge.v]){
print_road1(edge.v,e);
printf(" %d",u);
break;
}
}
}
}
void print_road2(int u,int e){
printf(" %d",u);
if(u==e) return;
else{
int L=solver.G[u].size();
for(int i=0;i<L;i++){
Edge& edge=solver.edges[solver.G[u][i]];
if(g[u]-edge.dist==g[edge.v]){
print_road2(edge.v,e);
break;
}
}
}
}
int main(){
freopen("in.txt","r",stdin);
int cas=0;
while(scanf("%d%d%d",&N,&S,&E)!=EOF){
if(cas++) puts("");
solver.init(N+1);
scanf("%d",&M);
int u,v,dist;
while(M--){
scanf("%d%d%d",&u,&v,&dist);
solver.add_edge(u,v,dist);
solver.add_edge(v,u,dist);
}
solver.dijkstra(S,f);
solver.dijkstra(E,g);
int ans=f[E],s=-1,e=-1;
scanf("%d",&K);
while(K--){
scanf("%d%d%d",&u,&v,&dist);
if(f[u]==INF||g[v]==INF) continue;
if(f[u]+g[v]+dist<ans){
ans=f[u]+g[v]+dist;
s=u;
e=v;
}
if(f[v]==INF||g[u]==INF) continue;
if(f[v]+g[u]+dist<ans){
ans=f[v]+g[u]+dist;
s=v;
e=u;
}
}
if(s==-1) print_road1(E,S);
else{
print_road1(s,S);
print_road2(e,E);
}
puts("");
if(s==-1) printf("Ticket Not Used\n");
else printf("%d\n",s);
printf("%d\n",ans);
}
return 0;
}