BZOJ#1491[NOI2007]社交网络
[NOI2007]社交网络
思路:
使用floyd算法并记录路径,
d[i][j]=d[i][k]*d[k][j]
代码:
#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
const int N = 110;
int g[N][N];
int d[N][N];
double ans[N];
void solve(int Case) {
int n, m;
scanf("%lld%lld", &n, &m);
memset(g, 0x3f, sizeof g);
for (; m--;) {
int a, b, w;
scanf("%lld%lld%lld", &a, &b, &w);
g[a][b] = g[b][a] = w;
d[a][b] = d[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 == j or j == k or i == k) continue;
if (g[i][j] > g[i][k] + g[k][j]) {
g[i][j] = g[i][k] + g[k][j];
d[i][j] = d[i][k] * d[k][j];
} else if (g[i][j] == g[i][k] + g[k][j]) {
d[i][j] += d[i][k] * d[k][j];
}
}
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j or j == k or i == k) continue;
if(g[i][j]==g[i][k]+g[k][j]){
ans[k]+=(1.0*d[i][k]*d[k][j])/d[i][j]*1.0;
}
}
}
}
for(int i=1;i<=n;i++){
printf("%.3lf\n",ans[i]);
}
}
signed main() {
//ios::sync_with_stdio(false); cin.tie(nullptr);
//cin >> _; for (Case = 1; Case <= _; Case++)
solve(Case);
return 0;
}