๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/10021
10021๋ฒ: Watering the Fields
Input Details There are 3 fields, at locations (0,2), (5,0), and (4,3). The contractor will only install pipes of cost at least 11. Output Details FJ cannot build a pipe between the fields at (4,3) and (5,0), since its cost would be only 10. He therefore b
www.acmicpc.net
Farmer John ์ ๋์์ ๋ชจ๋ field์ ๋ฌผ์ ์ฃผ๊ธฐ์ํด์ ํ์ํ ์ต์๋น์ฉ์ ์ถ๋ ฅํ์.
ํ์ด
์ผ๋จ ์์ด๋ฌธ์ ๋ผ์ ๋ฌธ์ ๋ฅผ ์ ํํ ํด์ํ๋๊ฒ ์ด๋ ค์ ๋ค.
๊ฐ ํ๋๋ฅผ ๋ ธ๋์ฒ๋ผ ์๊ฐํ๊ณ ๋ชจ๋ ํ๋๊ฐ ์ด์ด์ง๋๋ก ํด์ผํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ ํ๋์ฌ์ด์ ํ์ดํ ์ค์น ๋น์ฉ์ ์ฃผ์ด์ง ๊ณต์ (xi - xj)^2 + (yi - yj)^2 ๋ฅผ ์ด์ฉํด์ ๊ตฌํ๋ค.
ํ์ง๋ง ๊ฐ ํ์ดํ๋ฅผ ์ค์นํ ๋ ์ค์นํ๋ ์ฌ๋์ด ์ต์ ๋น์ฉ C ๋ณด๋ค ์ค์น๋น์ฉ์ด ๋ฎ๋ค๋ฉด ๊ทธ ๊ตฌ๊ฐ์๋ ์ค์น๋ฅผ ํด์ฃผ์ง ์๋๋ค.
๋ชจ๋ ๋ ธ๋๋ฅผ ์ต์ ๋น์ฉ์ผ๋ก ์ฐ๊ฒฐ์์ผ์ผ ํ๋ฏ๋ก "์ต์ ์คํจ๋ ํธ๋ฆฌ"๋ฅผ ์ฌ์ฉํ๋ค. ๋ชจ๋ ๋ ธ๋ ์ฌ์ด์ ์ค์น๋น์ฉ์ ๊ตฌํด์ C๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด ์ฐ์ ์์ ํ์ ๋ฃ๋๋ค.
์ด๋ ๊ฒ ํด์ ์ต์ ์คํจ๋ ํธ๋ฆฌ๋ฅผ ๋ง๋ค์ด์ฃผ๋ฉด ๋๋๋ฐ, ๋ง์ฝ ๋ชจ๋ ๋ ธ๋๋ฅผ ์ฐ๊ฒฐํ์ง ๋ชปํ๋ค๋ฉด -1์ ์ถ๋ ฅํด์ฃผ๋ฉด ๋๋ค. ์ฆ ๋ชจ๋ ๋ ธ๋์ Parent๊ฐ ๊ฐ์ผ๋ฉด ๋น์ฉ ์ถ๋ ฅ, ๊ฐ์ง ์์ผ๋ฉด -1์ ์ถ๋ ฅํ๋ฉด ๋๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> Priority_queue;
const int INF = INT_MAX;
const int MAX_N = 2000 + 5;
int N, C;
vector<pair<int,int>> Position;
int Parent[MAX_N];
int Find(int X) {
if (Parent[X] == X)
return X;
else return Parent[X] = Find(Parent[X]);
}
void Union(int x, int y) {
int parent_x = Find(x);
int parent_y = Find(y);
Parent[parent_x] = parent_y;
}
int get_cost(int A, int B) {
return (pow(Position[A].first - Position[B].first, 2) + pow(Position[A].second - Position[B].second, 2));
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> C;
for (int i = 0; i <= N; i++)
Parent[i] = i;
for (int i = 0; i < N; i++) {
int X, Y;
cin >> X >> Y;
Position.push_back({ Y,X });
}
Priority_queue pq;
for (int i = 0; i < Position.size() - 1; i++) {
for (int j = i + 1; j < Position.size(); j++) {
int cost = get_cost(i, j);
if (cost < C) continue;
pq.push({ cost,{i,j} });
}
}
int ans = 0;
while (!pq.empty()) {
int cost = pq.top().first;
int Node1 = pq.top().second.first;
int Node2 = pq.top().second.second;
pq.pop();
if (Find(Node1) != Find(Node2)) {
ans += cost;
Union(Node1, Node2);
}
else continue;
}
bool chk = true;
int Parent_zero = Find(0);
for (int i = 1; i < N; i++)
if (Parent_zero != Find(i)) {
chk = false;
break;
}
if (chk)
cout << ans << "\n";
else cout << "-1\n";
return 0;
}
|
cs |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 16639 : ๊ดํธ ์ถ๊ฐํ๊ธฐ 3 (0) | 2022.04.19 |
---|---|
[BOJ] 17780 : ์๋ก์ด ๊ฒ์ (0) | 2022.04.15 |
[BOJ] 15684 : ์ฌ๋ค๋ฆฌ ์กฐ์ (0) | 2022.04.14 |
[BOJ] 14499 : ์ฃผ์ฌ์ ๊ตด๋ฆฌ๊ธฐ (0) | 2022.04.08 |
[BOJ] 18866 : ์ ์ ๋ ์ ์์ด์ฌ (0) | 2022.04.08 |