-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemC.cpp
64 lines (55 loc) · 1.38 KB
/
ProblemC.cpp
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
62
63
64
#include <cstdio>
#include <algorithm>
const int MAXN = 1e6 + 10;
int n, m, k, fa[MAXN];
long long ans;
struct edge{
int u, v, w;
bool operator < (const edge &other) const {
return w > other.w;
}
} edges[MAXN];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void kruskal() {
std::sort(edges, edges + k);
for (int i = 0; i <= n * m; i++) fa[i] = i;
int cnt = 0;
for(int i = 0; i < k; i++) {
int u = edges[i].u, v = edges[i].v;
int fu = find(u), fv = find(v);
if(fu != fv) {
fa[fu] = fv;
ans += edges[i].w;
cnt++;
}
if(cnt == n * m - 1) break;
}
}
int main() {
scanf("%d%d", &n, &m);
int16_t map[n + 2][m + 2];
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
scanf("%hd", &map[i][j]);
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(j != m) {
edges[k].u = m * (i - 1) + j;
edges[k].v = m * (i - 1) + j + 1;
edges[k++].w = map[i][j] * map[i][j + 1];
}
if(i != n) {
edges[k].u = m * (i - 1) + j;
edges[k].v = m * i + j;
edges[k++].w = map[i + 1][j] * map[i][j];
}
}
}
kruskal();
printf("%lld\n", ans);
return 0;
}