社交网络

题目大意: 社交网络

看到数据范围果断想到弗洛伊德

但是有如下坑点:

  • 统计答案的时候一定要判断当前点是不是两断点对最短路...

  • 一个数组记录最短路,一个数组记录方案数目.

其中\(f_{i,j}\)表示从i到j的最短路对方案数目,\(g_{i,j}\)表示i到j对最短距离.

然后依题跑一趟就可以了.

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;

const int N = 1e2 + 66;

int n, m;
int g[N][N], f[N][N];

inline void pres_dou()
{
int i, j; memset(g, 0x3f, sizeof g);
for (i = 1; i <= n; ++ i) g[i][i] = 0;
for (i = 1; i <= n; ++ i) for (j = 1; j <= n; ++ j) f[i][j] = 1;
return;
}

signed main()
{
int i, j, k, x, y, z;
n = read(), m = read(), pres_dou();
for (i = 1; i <= m; ++ i)
{
x = read(), y = read(), z = read();
g[x][y] = g[y][x] = z;
}

for (k = 1; k <= n; ++ k) for (i = 1; i <= n; ++ i) for (j = 1; j <= n; ++ j)
{
if (i != k && i != j && k != j)
{
if (g[i][j] >= g[i][k] + g[k][j])
{
if (g[i][j] == g[i][k] + g[k][j]) f[i][j] += f[i][k] * f[k][j];
else
{
g[i][j] = g[i][k] + g[k][j];
f[i][j] = f[i][k] * f[k][j];
}
}
}
}

for (k = 1; k <= n; ++ k)
{
double res(0);
for (i = 1; i <= n; ++ i)
{
for (j = 1; j <= n; ++ j)
{
if (i == k || j == k || ! f[i][j]) continue;
if (g[i][k] + g[k][j] == g[i][j]) res += 1.0 * f[i][k] * f[k][j] / f[i][j];
}
}
printf ("%.3lf\n", res);
}

return 0;
}

后记:

考场代码(30pts)距离AC只差两句话对距离.

弗洛伊德不需要用到inf,少用inf