Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

16-yuyu0830 #62

Merged
merged 2 commits into from
Jul 5, 2024
Merged

16-yuyu0830 #62

merged 2 commits into from
Jul 5, 2024

Conversation

yuyu0830
Copy link
Collaborator

@yuyu0830 yuyu0830 commented Jun 4, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ณ„์ž๋ฆฌ ๋งŒ๋“ค๊ธฐ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

5-10๋ถ„!

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

โ“ ๋ฌธ์ œ

๋ณ„์˜ ์œ„์น˜ (float x, float y) ๊ฐ€ n๊ฐœ ์ฃผ์–ด์ง„๋‹ค. ๋ชจ๋“  ๋ณ„์„ ํฌํ•จํ•˜๋ฉด์„œ ๊ทธ ๊ธธ์ด๊ฐ€ ๊ฐ€์žฅ ์งง์€ ๋ณ„์ž๋ฆฌ๋ฅผ ๊ทธ๋ ค๋ณด์ž!

โ— ํ’€์ด

๋„ˆ๋ฌด ๊ฐ„๋‹จํ•œ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ ๋ฌธ์ œ.. ๋‚ ๋จน ๋Š๋‚Œ์ด ์กฐ๊ธˆ ๋‚˜์ง€๋งŒ ์ตœ๊ทผ์— 3๋ฌธ์ œ ํ‘ธ๋Š”๋ฐ 100๋ฒˆ ๊ฐ€๊นŒ์ด ํ‹€๋ฆฌ๊ณ  ๋ฉ˜ํƒˆ์ด ๊ฐˆ๋ ค์„œ ์‚ด์ง ์‰ฌ์–ด๊ฐ€๋Š” ๋Š๋‚Œ์œผ๋กœ ํ’€์–ด๋ดค๋‹ค. ๊ธฐ์กด์— MST ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ์ผ๋˜ Union-Find ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ํ–ˆ๋‹ค.

๋ณ„์˜ ์œ„์น˜๊ฐ€ n๊ฐœ ์ฃผ์–ด์ง€๋ฉด ๊ฐ ๋ณ„๋ผ๋ฆฌ ์ด์„ ์ˆ˜ ์žˆ๋Š” ์„ ๋ถ„์˜ ๊ฐœ์ˆ˜๋Š” n * (n - 1) / 2 ๊ฐ€ ๋œ๋‹ค. ํ•˜์ง€๋งŒ ๋ณ„์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ๋Œ€ 100๊ฐœ ๋ฐ–์— ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ์„ ๋ถ„์„ ๊ณ ๋ คํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์งฐ๋‹ค.

์šฐ์„  ๊ฐ ๋ณ„์˜ ์œ„์น˜๋ฅผ ์ „๋ถ€ ๋ฐ›์€ ๋’ค, ๊ฐ ๋ณ„๋งˆ๋‹ค ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์„ ๋ถ„์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ €์žฅํ•ด ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•œ๋‹ค.

// ๋ณ„ ์œ„์น˜ ์ €์žฅ
for (int i = 0; i < n; i++) {
    float a, b; cin >> a >> b;
    arr[i] = point(a, b);
}

// ๊ฐ ๋ณ„๋งˆ๋‹ค ์ด์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์„  ์šฐ์„  ์ˆœ์œ„ ํ์— ์ €์žฅ
// ์šฐ์„  ์ˆœ์œ„ ํ๋Š” ์„ ๋ถ„์˜ ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
for (int i = 0; i < n; i++) 
    for (int j = i + 1; j < n; j++) 
        q.push(edge(i, j));

์ดํ›„์— Union-Find๋ฅผ ์‚ฌ์šฉํ•œ MST ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉํ•˜๋ฉด ์™„์„ฑ!

#include <iostream>
#include <queue>
#include <math.h>

#define fastio cin.tie(NULL); cin.sync_with_stdio(false);

using namespace std;

struct point {
    float x, y;
    point(float X, float Y) : x(X), y(Y) {};
    point() {};
};

point arr[10000];

struct edge {
    int vertex1, vertex2;
    float value;
    edge(int v1, int v2) : vertex1(v1), vertex2(v2) {
        value = sqrt(pow(arr[v1].x - arr[v2].x, 2) + pow(arr[v1].y - arr[v2].y, 2));
    }
};

struct cmp {
    bool operator()(edge a, edge b) { return a.value > b.value; }
};

int parent[101] = {0, };

int find(int v) {
    if (parent[v] == v) return v;
    return parent[v] = find(parent[v]);
}

void merge(int x, int y) { parent[find(x)] = find(y); }

bool isUnion(int x, int y) { return find(x) == find(y); }

int main() {
    fastio
    int n; cin >> n;

    for (int i = 1; i <= n; i++)
        parent[i] = i;
    
    priority_queue<edge, vector<edge>, cmp> q;

    // ๋ณ„ ์œ„์น˜ ์ €์žฅ
    for (int i = 0; i < n; i++) {
        float a, b; cin >> a >> b;
        arr[i] = point(a, b);
    }

    // ๊ฐ ๋ณ„๋งˆ๋‹ค ์ด์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์„  ์šฐ์„  ์ˆœ์œ„ ํ์— ์ €์žฅ
    // ์šฐ์„  ์ˆœ์œ„ ํ๋Š” ์„ ๋ถ„์˜ ๊ธธ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
    for (int i = 0; i < n; i++) 
        for (int j = i + 1; j < n; j++) 
            q.push(edge(i, j));
        
    int cnt = 1;
    float ans = 0.f;

    while (cnt != n) {
        edge tmp = q.top();
        q.pop();

        int v1 = tmp.vertex1;
        int v2 = tmp.vertex2;

        if (isUnion(v1, v2)) continue;

        merge(v1, v2);
        cnt++;
        ans += tmp.value;

        if (cnt == n) {
            printf("%.2f\n", ans);
            return 0;
        }
    }
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ—คํ—ค ๋‹ฌ๋‹ค

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌด๋‚œํ•œ ์ŠคํŒจ๋‹ํŠธ๋ฆฌ ๋ฌธ์ œ๊ตฐ๋‡จ..

์—ฌ๊ธฐ์„œ ์›ƒ๊ฒผ๋˜์ ์€ ์†Œ์ˆ˜์  ์ž๋ฆฌ๋ฅผ ํ‘œํ˜„ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ cout์ด ๋ถˆํŽธํ•˜๋‹ˆ๊ฐ€ ๊ฐ„ํŽธํ•œ printf๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์ ์ด ๋‹๋ณด์˜€์Šต๋‹ˆ๋‹ค.

union find ๊ด€ํ•ด์„œ ๊ฐ€๋”์”ฉ ์ฝ”ํ…Œ์—์„œ๋„ ๋‚˜์˜ค๋”๋ผ๊ตฌ์š”. union-find๋Š” ์ŠคํŒจ๋‹ํŠธ๋ฆฌ ์ด์™ธ์—๋„ ๋‹ค๋ฅธ ๊ณณ์—์„œ๋„ ๋‹ค์–‘ํ•˜๊ฒŒ ์“ฐ์ผ ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฒ•์ด๋‹ˆ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฉด์œผ๋กœ ์ˆ™์ง€ํ•ด๋‘๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šด๋‹ค.

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  17๋ฒˆ pr๋ฅผ ๋ฐ”๋กœ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž ์ตœ์†Œ์ŠคํŒจ๋‹ํŠธ๋ฆฌ ์žฌ๋ฐŒ๋„ค์š”. ๋ณ„์ž๋ฆฌ์ˆ˜์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๋จผ์ € ํŒŒ์•…ํ•˜๊ณ  ๋ชจ๋“  ์„ ๋ถ„์„ ์ฒดํฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌ์ƒํ•œ ๋ถ€๋ถ„์ด ์ œ๊ฐ€ ๋ฐฐ์šธ ์ ์ด์—ˆ์–ด์š”!

@yuyu0830 yuyu0830 merged commit 4229c80 into main Jul 5, 2024
@yuyu0830 yuyu0830 deleted the 16-yuyu0830 branch July 5, 2024 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants