The main topic: The Airport Express line is divided into two kinds of economic and commercial lines, line, speed and price are different. You have a commercial line ticket, you can take a commercial line, and other times you can only take the economic line
Now give you the starting point and the end, asking you to find the quickest route
Problem-solving ideas: I have to say, this problem is still a bit disgusting
To make two Dijkstra, one at a time starting from the source, the resulting array D1 represents the closest distance between the node and the starting point
Another time with the endpoint as the source point, get the array D2, representing the closest distance between the node and the end point
Now M business ticket, given the location of X, Y, Z
Then there are two options, one is to go from the starting point to X, then use a commercial ticket, and then go from Y to the end, then the distance is d1[x] + z + d2[y]
Another way is to go from the starting point to Y, then use the commercial ticket, then from Y to the end, then the distance is d1[y] + z + d2[x]
Find the minimum value.
Then there's the disgusting output path problem.
#include <cstdio>#include <cstring>#include <algorithm>using namespace Std;#define N 510#define M 1010#define INF 0x3f3f3f3fstruct node{int x,y;} NODE[M];intDis[n][n], D1[n], d2[n], pre1[n], Pre2[n];bool vis[n];intNsEm, K;void init () {scanf ("%d", &m); memset (DIS,0x3f, sizeof (DIS));int x,y, Z; for(inti =0; I <m; i++) {scanf (" %d%d%d", &x, &y, &z); dis[x][y] = dis[y][x] = min (dis[x][y], z); }}void Dijstra (int s,int *d,int *pre) {memset (Vis,0, sizeof (VIS)); for(inti =1; I <= N; i++) D[i] = INF; d[s] =0; for(inti =1; I <= N-1; i++) {int x, t = INF; for(intj =1; J <= N; J + +) {if(!vis[j] && d[j] < T) {t = d[j];x= J; }} vis[x] =1; for(intj =1; J <= N; J + +)if(D[j] > d[x] + dis[x][j]) {D[j] = d[x] + dis[x][J]; PRE[J] =x; }}}void Print_path (int POS,intFlag) {intCNT =1, Ans[n];if(POS== -1) {ans[0] = e;intt = e; while(Pre1[t]! =s) {ans[cnt++] = pre1[t]; t = pre1[t]; }printf("%d",s); for(inti = cnt-1; I >=0; i--)printf(" %d", Ans[i]);printf("\ n");return; }int x,y;if(Flag = =0) {x= node[POS].x;y= node[POS].y; }Else{x= node[POS].y;y= node[POS].x; }if(x!=s) {ans[0] =x; while(pre1[x] !=s) {ans[cnt++] = pre1[x];x= pre1[x]; }printf("%d",s); for(inti = cnt-1; I >=0; i--)printf(" %d", Ans[i]); }Else{printf("%d",s); }if(y! = e) {printf(" %d",y); while(pre2[y]! = e) {printf(" %d", pre2[y]);y= pre2[y]; }printf(" %d", e); }Else{printf(" %d",y); }printf("\ n");return;} void Solve () {Dijstra (s, D1, pre1); Dijstra (E, D2, Pre2); scanf"%d", &k);int POS= -1, flag =0, Min = D1[e];int x,y, Z; for(inti =0; I < K; i++) {scanf (" %d%d%d", &x, &y, &z); Node[i].x=x; Node[i].y=y;if(Min > d1[x] + z + d2[y]) {POS= i; Flag =0; Min = d1[x] + z + d2[y]; }if(Min > d1[y] + z + d2[x]) {POS= i; Flag =1; Min = d1[y] + z + d2[x]; }} print_path (POS, flag);if(POS== -1)printf("Ticket not used\n");Else{if(Flag = =0)printf("%d\ n", node[POS].x);Else printf("%d\ n", node[POS].y); }printf("%d\ n", Min);}intMain () {bool flag = false; while(SCANF (" %d%d%d", &n, &s, &e)! = EOF) {if(flag)printf("\ n"); Flag = true; Init (); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-11374 Airport Express (Dijkstra)