floyd...求最短路时顺便求出路径数. 时间复杂度O(N^3)
-------------------------------------------------------------------------------------------
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 109;
const int INF = 0X3F3F3F3F;
int N, d[maxn][maxn];
ll cnt[maxn][maxn];
int main() {
int m;
scanf("%d%d", &N, &m);
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++)
d[i][j] = INF, cnt[i][j] = 0;
d[i][i] = 1;
cnt[i][i] = 1;
}
while(m--) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
u--, v--;
d[u][v] = d[v][u] = w;
cnt[u][v] = cnt[v][u] = 1;
}
for(int k = 0; k < N; k++)
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
if(d[i][k] + d[k][j] < d[i][j]) {
d[i][j] = d[i][k] + d[k][j];
cnt[i][j] = cnt[i][k] * cnt[k][j];
} else if(d[i][k] + d[k][j] == d[i][j])
cnt[i][j] += cnt[i][k] * cnt[k][j];
for(int k = 0; k < N; k++) {
double ans = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++) if(d[i][j] != INF)
if(d[i][k] + d[j][k] == d[i][j])
ans += (double) cnt[i][k] * cnt[j][k] / cnt[i][j];
printf("%.3lf\n", ans);
}
return 0;
}
-------------------------------------------------------------------------------------------
1491: [NOI2007]社交网络
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1271 Solved: 727
[Submit][Status][Discuss]
Description
Input
Output
输出文件包括n 行,每行一个实数,精确到小数点后3 位。第i 行的实数表 示结点i 在社交网络中的重要程度。
Sample Input
4 4
1 2 1
2 3 1
3 4 1
4 1 1
1 2 1
2 3 1
3 4 1
4 1 1
Sample Output
1.000
1.000
1.000
1.000
1.000
1.000
1.000
HINT
为1
Source