floyd。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxv 150 #define inf 1000000007 using namespace std; int n,m,x,y; double map[maxv][maxv],z,g[maxv][maxv]; int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { if (i==j) {map[i][j]=0;g[i][j]=1;} else {map[i][j]=inf;g[i][j]=0;} } for (int i=1;i<=m;i++) { scanf("%d%d%lf",&x,&y,&z); map[x][y]=map[y][x]=z; g[x][y]=g[y][x]=1; } for (int k=1;k<=n;k++) for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { if (map[i][j]>map[i][k]+map[k][j]) { map[i][j]=map[i][k]+map[k][j]; g[i][j]=g[i][k]*g[k][j]; } else if (map[i][j]==map[i][k]+map[k][j]) if ((i!=k) && (j!=k)) g[i][j]+=g[i][k]*g[k][j]; } for (int i=1;i<=n;i++) { double ans=0; for (int j=1;j<=n;j++) for (int k=1;k<=n;k++) { if ((i!=j) && (i!=k)) { if (map[j][i]+map[i][k]==map[j][k]) ans+=(g[j][i]*g[i][k])/g[j][k]; } } printf("%.3lf\n",ans); } return 0; }