Problem-solving ideas: the use of Dijkstra algorithm, counted twice, once to count the shortest time, the shortest path first calculation, another record array to open a path
#include <stdio.h> #include < String .h> #define INF 0x3f3f3f3f #define MaxVex 500 typedef struct { int length; int Time; } Graph; Graph G [MaxVex] [MaxVex]; // FIG int Visit [MaxVex ] = { 0 }; // access tag int fpath [MaxVex]; // fastest route int LPATH [MaxVex]; // shortest path int NUM [MaxVex] = { 0 }; // node count int Nv, Ne; //图初始化-邻接矩阵 void Init() { int i,j; for(i=0; i<Nv; i++) { for(j=0; j<Nv; j++) { G[i][j].length=INF; G[i][j].time=INF; } } int v1,v2,one_way,length,time; for(i=0; i<Ne; i++) { scanf("%d %d %d %d %d",&v1,&v2,&one_way,&length,&time); G[v1][v2].length=length; G [V1] [V2] .time =Time; IF (! One_way) { G [V2] [V1] = G [V1] [V2]; } } } void the Dijkstra ( int S, int type) { Visit [S] = . 1 ; NUM [S] = . 1 ; int I, J, W, MIN; // calculate the fastest route, the fastest route if not unique, then the shortest path is selected IF (type == . 1 ) { for (I = 0 ; I <Nv; I ++ ) { MIN = INF; for (J = 0 ; J <Nv; J ++) { if(!visit[j]&&G[s][j].time<MIN) { MIN=G[s][j].time; w=j; } } visit[w]=1; for(j=0; j<Nv; j++) { if(!visit[j]&&MIN+G[w][j].time<G[s][j].time) { G[s][j].time=MIN+G[w][j].time; fpath[j]=w; } else if(! Visit [J] && MIN + G [W] [J] .time == G [S] [J] .time) { // note here is that the shortest path G [w] [j] .length <G [ fpath [J]] [J] .length // rather than G [S] [W] .length + G [W] [J] .length <G [S] [J] .length IF (G [W] [ J] .length < G [fpath [J]] [J] .length) { fpath [J] = W; } } } } } the else IF (type == 2 ) { // calculate the shortest path, if not the only, the first node is less for (J = 0 ; J <Nv; J ++ ) { MIN = INF; for (I = 0; i<Nv; i++) { if(!visit[i]&&G[s][i].length<MIN) { MIN=G[s][i].length; w=i; } } visit[w]=1; for(i=0; i<Nv; i++) { if(!visit[i]&&MIN+G[w][i].length<G[s][i].length) { G[s][i].length=MIN+G[w][i].length; lpath[i]=w; num[i]NUM = [W] + . 1 ; } the else IF (! Visit [I] && MIN + G [W] [I] .length == G [S] [I] .length) { IF (NUM [W] + . 1 < NUM [I]) { NUM [I] = NUM [W] + . 1 ; LPATH [I] = W; } } } } } } // determines whether the two paths are not the same int IsTheSame ( int A [], int n-, int B [], int m) { IF(n!=m) return 0; else { int i; for(i=0; i<=n; i++) { if(a[i]!=b[i]) return 0; } return 1; } } int main() { scanf("%d %d",&Nv,&Ne); Init(); int s,d,i; scanf("%d %d",&s,&d); memset(visit,0 , the sizeof (Visit)); for (I = 0 ; I <Nv; I ++ ) { fpath [I] = S; } the Dijkstra (S, . 1 ); Memset (Visit, 0 , the sizeof (Visit)); for ( = I 0 ; I <Nv; I ++ ) { LPATH [I] = S; } the Dijkstra (S, 2 ); // reverse output path into an array int F = D, L = D; int FRoad [MaxVex] = { 0 }, LRoad [MaxVex] = { 0 }; int t=0,k=0; while(f!=s) { FRoad[t++]=f; f=fpath[f]; } FRoad[t]=s; while(l!=s) { LRoad[k++]=l; l=lpath[l]; } LRoad[k]=s; //结果输出 if(IsTheSame(FRoad,t,LRoad,k)) { printf("Time = %d; Distance = %d: ",G[s][d].time,G[s][d].length); for(i=t; i>=0; i--) { printf("%d",FRoad[i]); if(i) printf(" => "); } } else { printf("Time = %d: ",G[s][d].time); for(i=t; i>=0; i--) { printf("%d",FRoad[i]); if(i) printf(" => "); } printf("\n"); printf("Distance = %d: ",G[s][d].length); for(i=k; i>=0; i--) { printf("%d",LRoad[i]); if(i) printf(" => "); } } }