-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathKarivHakimi.cc
61 lines (58 loc) · 1.57 KB
/
KarivHakimi.cc
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
61
/**
* 求图的绝对中心, 可用在求最小直径生成树
*
* g[][]为邻接矩阵, 把没有的边权赋为inf
* 返回一个pair, 表示绝对中心在这条边(s1, s2)上
* ds1, ds2记录s1和s2距离绝对中心的距离, 按需返回
**/
#include <algorithm>
const int N = 1000, inf = 1e9;
void floyd(int n, int g[][N], int d[][N]) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
d[i][j] = g[i][j];
}
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
g[i][j] = std::min(g[i][j], g[i][k] + g[k][j]);
}
}
}
}
std::pair<int, int> KarivHakimi(int n, int g[][N]) {
static int rk[N][N], d[N][N];
double ds1 = 0, ds2 = 0;
floyd(n, g, d);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) rk[i][j] = j;
std::sort(rk[i], rk[i] + n, [&](int a, int b) {
return d[i][a] < d[i][b];
});
}
int ret = inf, s1 = -1, s2 = -1;
for (int u = 0; u < n; ++u) {
if (d[u][rk[u][n - 1]] * 2 < ret) {
ret = d[u][rk[u][n - 1]] * 2;
s1 = s2 = u;
ds1 = ds2 = 0;
}
for (int v = 0; v < n; ++v) {
if (g[u][v] == inf) continue;
for (int k = n - 1, i = n - 2; i >= 0; --i) {
if (d[v][rk[u][i]] > d[v][rk[u][k]]) {
int tmp = d[u][rk[u][i]] + d[v][rk[u][k]] + g[u][v];
if (tmp < ret) {
ret = tmp;
s1 = u, s2 = v;
ds1 = 0.5 * tmp - d[u][rk[u][i]];
ds2 = g[u][v] - ds1;
}
k = i;
}
}
}
}
return std::make_pair(s1, s2);
}