Both sides Dijstra, finally find out and the largest, it is worth noting that the individual path must pay attention to the direction, or is wrong. ***
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<queue>#include<algorithm>using namespacestd;#defineN 1010#defineINF 0x3f3f3f3fintMaps[n][n];intN, m, X;intDist1[n], dist2[n], vis[n];voidInit () { for(intI=1; i<=n; i++) for(intj=1; j<=n; J + +) Maps[i][j]=i==j?0: INF;}voidDij () { for(intI=1; i<=n; i++) Dist1[i]=Maps[i][x]; memset (Vis,0,sizeof(VIS)); VIS[X]=1; for(intI=1; i<=n; i++) { intMin=inf, index=-1; for(intj=1; j<=n; J + +) { if(!vis[j]&&dist1[j]<Min) {Min=Dist1[j]; Index=J; } } if(index==-1) Break; Vis[index]=1; for(intj=1; j<=n; J + +) { if(!vis[j]&&dist1[j]>maps[j][index]+Dist1[index]) dist1[j]=maps[j][index]+Dist1[index]; } } for(intI=1; i<=n; i++) Dist2[i]=Maps[x][i]; memset (Vis,0,sizeof(VIS)); VIS[X]=1; for(intI=1; i<=n; i++) { intMin=inf, index=-1; for(intj=1; j<=n; J + +) { if(!vis[j]&&dist2[j]<Min) {Min=Dist2[j]; Index=J; } } if(index==-1) Break; Vis[index]=1; for(intj=1; j<=n; J + +) { if(!vis[j]&&dist2[j]>dist2[index]+Maps[index][j]) dist2[j]=dist2[index]+Maps[index][j]; } }}intMain () { while(~SCANF ("%d%d%d", &n, &m, &x)) {Init (); intu, V, Len; for(intI=1; i<=m; i++) {scanf ("%d%d%d", &u, &v, &Len); MAPS[U][V]=min (len, maps[u][v]); } dij (); intans=-1; for(intI=1; i<=n; i++) { if(dist1[i]+dist2[i]>ans) ans=dist1[i]+Dist2[i]; } printf ("%d\n", ans); } return 0;}
POJ 3268 Shortest circuit