Airport Express
In a small city called Iokh, a train service, Airport-express, takes residents to the Airport more quickly than other Tran Sports. There is types of trains in Airport-express, the economy-xpress and the commercial-xpress. They travel at different speeds, take different routes and has different costs.
Jason is going to the airport to meet his friend. He wants to take the commercial-xpress which was supposed to being faster, but he doesn ' t has 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 are not easy for him. Jason now seeks your help. The routes of the types of trains is given. Please write a program to find the best route to the destination. The program should also the ticket should is used.
Input
The input consists of several test cases. Consecutive cases is separated by a blank line. The first line of all 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 is connected and it takes Z minutes to travel between these-stations. The next line is another integer K (1≤k≤1000) representing the number of connections between the stations of the Comme Rcial-xpress. The next K lines contain the information of the commercialxpress in the same format as that of the economy-xpress. All connections is bi-directional. You could assume that there was exactly one optimal route to the Airport. There might is cases where you must use your ticket on 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 your 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 in the last line. Consecutive sets of output must is 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
Output format:
For each set of data, output 3 rows. The first line indicates the individual stations (including the start and end points) that were passed in the order of access, and the second line is the station number of the interchange commercial line (if no business insurance ticket, output ticket not used), the third row output total time
Analysis:
The commercial station can only sit one stop and cite all possible
such as commercial stations from a to B, then from the starting point to a, from a to the end of the two route should be the shortest, so we just start from seven o'clock, to the end of the two single-source shortest path, record from the beginning to each node x of the shortest time f (x) and the minimum time from each point to the end of the G (X), the total time is F ) +t (A, b) +f
where T (A, A, b) represents the time taken for a to commercial route
#include <iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<cmath>#include<stack>#include<vector>#include<queue>#include<string>#include<algorithm>Const intINF =0x3f3f3f;Const intMAXN = 5e2+Ten;Const intMaxnn = 1e3+Ten;structedge{intSt; intto ; intDist; intnext;};structheapnode{//Priority Queue Node intSt; intDist; BOOL operator< (Constheapnode& RHS)Const { returnDist>rhs.dist; }};using namespacestd;intFirst[maxn];edge e[2*Maxnn];inttop;intD1[MAXN];//->sintD2[MAXN];//->eintp1[2*MAXN];intp2[2*MAXN];intDONE[MAXN];intn,m,k;intCs,ce;intNA,NB;voidinit () {memset (First,-1,sizeof(first)); Top=0;}voidAddedge (intUintVintDist) {E[top].st=u; E[top].to=v; E[top].dist=Dist; E[top].next=First[u]; First[u]= top++;}voiddijkstra_s (ints) {Priority_queuep; Heapnode a,tmp; memset (Done,0,sizeof(done)); for(intI=0; i<n;i++) {D1[i]=inf; } D1[s]=0; A.St=s; A.dist=0; Q.push (a); intu; while(!Q.empty ()) {tmp=Q.top (); Q.pop (); U=Tmp.st; if(Done[u])Continue; Done[u]=1; for(inti=first[u];i!=-1; i=E[i].next) { if(d1[e[i].to]>d1[u]+e[i].dist) {D1[e[i].to]= d1[u]+e[i].dist; P1[e[i].to]=i; A.St=e[i].to; A.dist=D1[e[i].to]; Q.push (a); } } }}voidDijkstra_e (ints) {Priority_queuep; Heapnode a,tmp; memset (Done,0,sizeof(done)); for(intI=0; i<n;i++) {D2[i]=inf; } D2[s]=0; A.St=s; A.dist=0; Q.push (a); intu; while(!Q.empty ()) {tmp=Q.top (); Q.pop (); U=Tmp.st; if(Done[u])Continue; Done[u]=1; for(inti=first[u];i!=-1; i=E[i].next) { if(d2[e[i].to]>d2[u]+e[i].dist) {D2[e[i].to]= d2[u]+e[i].dist; P2[e[i].to]=i; A.St=e[i].to; A.dist=D2[e[i].to]; Q.push (a); } } }}voidPrintA (intu) { if(u==CS) {cout<<u+1; return; } PrintA (E[p1[u]].st); cout<<" "<<u+1;}voidPRINTB (intu) { if(u==CE) {cout<<" "<<u+1<<Endl; return ; } cout<<" "<<u+1; PRINTB (E[p2[u]].st);}intMain () {intA,B,TD; intMc=0; while(SCANF ("%d%d%d", &n,&cs,&ce)! =EOF) { if(mc++) cout<<Endl; Init (); scanf ("%d",&m); for(intI=0; i<m;i++) {scanf ("%d%d%d",&a,&b,&TD); A--; b--; Addedge (A,B,TD); Addedge (B,A,TD); } CS--; Ce--; dijkstra_s (CS); Dijkstra_e (CE); intAns =D1[ce]; Na= NB =-1; /*cout<< "* * *" <<endl; cout<<ans<<endl; cout<<d2[cs]<<endl; cout<< "* * *" <<endl;*/scanf ("%d",&k); for(intI=0; i<k;i++) {scanf ("%d%d%d",&a,&b,&TD); A--; b--; if(d1[a]+d2[b]+td<ans) {na=A; NB=b; Ans= d1[a]+d2[b]+TD; } if(d1[b]+d2[a]+td<ans) {na=b; NB=A; Ans= d1[b]+d2[a]+TD; } } if(na==-1) {PrintA (CE); cout<<Endl; }Else{PrintA (NA); PRINTB (NB); } if(ans!=D1[ce]) {cout<<na+1<<Endl; }Else{cout<<"Ticket not used"<<Endl; } cout<<ans<<Endl; //cout<<endl; } //cout << "Hello world!" << Endl; return 0;}
View Code
UVA 11374 Dijkstra preprocessing + enumeration