题目链接
题解
n^3枚举一下就没了...
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
inline int read() {
int x = 0,f = 1 ;
char c = getchar();
while(c < '0' || c > '9')c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
int n,m;
const int maxn = 1007;
int mp[maxn][maxn];
long long cnt[maxn][maxn];
double ans[maxn];
int main() {
n = read(),m = read();
memset(mp,0x3f,sizeof mp);
for(int a,b,j = 1;j <= m;++ j) {
a = read(),b = read();
mp[a][b] = mp[b][a] = read();
cnt[a][b] = cnt[b][a] = 1;
}
for(int k = 1;k <= n;++ k)
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= n;++ j) {
if(i == k || k == j) continue;
if(mp[i][j] > mp[i][k] + mp[k][j]) {
mp[i][j] = mp[i][k] + mp[k][j];
cnt[i][j] = cnt[i][k] * cnt[k][j];
}
else if(mp[i][j] == mp[i][k] + mp[k][j])
cnt[i][j] += cnt[i][k] * cnt[k][j];
}
for(int i = 1;i <= n;++ i) cnt[i][i] = 0 ;
for(int x = 1;x <= n;++ x)
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= n;++ j)
if(mp[i][j] == mp[i][x] + mp[x][j] && cnt[i][j])
ans[x] += (double )cnt[i][x] * cnt[x][j] / (double) cnt[i][j];
for(int i = 1;i <= n;++ i) printf("%.3lf
",ans[i]);
return 0;
}