[BOJ] 10021 Watering the Fields

Time Lapse :16min 48sec

10021.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
#include <iostream>
#include <memory.h>
#include <cmath>
#include <queue>
using namespace std;
int n, c;
struct edge {
int f, e, c;
};
bool operator< (edge a, edge b) {
return a.c > b.c;
}
priority_queue<edge> pq;
pair<int, int> pos[2000];
int g[2000];
int getG(int cur) {
return g[cur] == cur ? cur : g[cur] = getG(g[cur]);
}

bool makeG(int a, int b) {
int ga = getG(a);
int gb = getG(b);
if(ga == gb)
return false;
g[ga] = g[gb] = min(ga,gb);
return true;
}
int distance(int i, int j) {
return abs(pos[i].first - pos[j].first) * abs(pos[i].first - pos[j].first) + abs(pos[i].second - pos[j].second) * abs(pos[i].second - pos[j].second);
}
int main() {
scanf("%d %d", &n, &c);
for (int i = 0; i < n; i++) {
scanf("%d %d",&pos[i].first, &pos[i].second);
for(int j = 0; j < i; j++) {
int d = distance(i, j);
if(d >= c)
pq.push({.f = i, .e = j, .c = d});
}
}
long long ans = 0;
for(int i = 0; i < n; i++)
g[i] = i;
while(!pq.empty()) {
edge e = pq.top();
pq.pop();
if(makeG(e.f, e.e))
ans += e.c;
}
for(int i = 0; i < n; i++)
if(g[i]) {
printf("-1");
exit(0);
}
printf("%lld",ans);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/09/04/PS/BOJ/10021/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.