Lying trough .... Recently brushed CF on the shortest way, originally wanted to take this topic to review ....
Test instructions is in the case of the output shortest, after each node will increase the tax, in addition to the dictionary output, note A to B and B to a weight difference
Then there is the problem of dictionary ordering, and when the same value is found in relaxation, the dictionary order of two paths is judged
Code
#include "stdio.h" const int maxn=110;const int Inf=10000000;bool vis[maxn];int pre[maxn];int cost[maxn][maxn],lowcost[ Maxn],b1[maxn];int road[maxn];using namespace Std;int cmp (int a,int b,int c) {int i,len1,len2,j; int P1[MAXN],P2[MAXN]; int cur=a; Pre[a]=b; len1=0; while (cur!=-1) {p1[len1++]=cur; Cur=pre[cur]; } len2=0; Cur=a; Pre[a]=c; while (cur!=-1) {p2[len2++]=cur; Cur=pre[cur]; } for (I=len1-1,j=len2-1;i>=0&&j>=0;i--, j--) {if (P1[i]>p2[j]) return C; if (P1[i]<p2[j]) return B; } if (i==-1) return B; if (j==-1) return C;} void Dijkstra (int beg,int n) {int ans; int k=0; for (int i=0; i<n; i++) {lowcost[i]=cost[beg][i]; Vis[i]=false; Pre[i]=-1; } for (int j=0; j<n; J + +) {int k; int min=inf; for (int i=0; i<n; i++) if (!vis[i]&&lowcost[i]≪min) {Min=lowcost[i]; K=i; } if (k==-1) break; Vis[k]=true; for (int i=0; i<n; i++) {if (cost[k][i]==inf) continue; if (!vis[i]&&lowcost[k]+cost[k][i]+b1[k]<lowcost[i]) {lowcost[i]=lowcost[k]+cost[k][i ]+B1[K]; Pre[i]=k; } else if (!vis[i]&&lowcost[k]+cost[k][i]+b1[k]==lowcost[i]) pre[i]=cmp (i,pre[i],k); }}}void print (int cur) {if (pre[cur]!=-1) print (pre[cur]); printf ("-->%d", cur+1);} int main () {int n,i,j; int a,b,c; int Ans,len; while (scanf ("%d", &n) ==1,n) {for (i=0; i<n; i++) {for (j=0; j<n; J + +) { scanf ("%d", &cost[i][j]); if (cost[i][j]==-1) Cost[i][j]=inf; }} for (i=0; i<n; i++) scanf("%d", &b1[i]); while (scanf ("%d%d", &a,&b) ==2) {if (a==-1&&b==-1) break; A-=1; B-=1; len=0; Dijkstra (A,n); ANS=LOWCOST[B]; printf ("From%d to%d: \npath:%d", a+1,b+1,a+1); if (a!=b) print (b); printf ("\ n"); printf ("Total Cost:%d\n\n", ans); }} return 0;}
Here is a small episode, this question and zoj a problem, the search hdoj, out of the Floyd and Dij above the solution of violence, search Zoj problem number, out of the Dij+dfs solution, the ...
Hdoj 1385Minimum Transport Cost