๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์ค€/Gold

[BOJ] 10021 : Watering the Fields

by Jaeguk 2022. 4. 14.
๋ฌธ์ œ ๋งํฌ

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<intpair<intint>>vector<pair<intpair<intint>>>, greater<pair<intpair<intint>>>> 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

728x90