#include <iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#include<vector>#include<queue>#include<cmath>#include<cstring>using namespacestd;#defineINF 0XFFFFFFF#defineMAXN 40intG[MAXN][MAXN], PATH[MAXN][MAXN], N;voidFloyd () { for(intk=1; k<=n; k++) { for(intI=1; i<=n; i++) { for(intj=1; j<=n; J + +) { if(G[i][j] > G[i][k] +G[k][j]) {G[i][j]= G[i][k] +G[k][j]; PATH[I][J]=Path[i][k]; } } } }}voidPutpath (intStar,intEnd) { while(Star! =End) {printf ("%d---->", Star); Star=Path[star][end]; } printf ("%d\n", End);}voidInit () { for(intI=1; i<=n; i++) { for(intj=1; j<=n; J + +) {Path[i][j]=J; } }}intMain () {CIN>>N; Init (); for(intI=1; i<=n; i++) { for(intj=1; j<=n; J + +) {cin>>G[i][j]; if(G[i][j] = =-1) G[i][j]=INF; }} Floyd (); Putpath (1, N); printf ("%d\n", g[1][n]); return 0;}/*4-1 1-1 -1-1-1 1-1-1-1-1 1-1 -1-1-1*/
Floyd algorithm Print path template