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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion yuyu0830/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
| 5์ฐจ์‹œ | 2024.03.29 | ํƒ์ƒ‰ | [์Šค๋„์ฟ ](https://www.acmicpc.net/problem/2239) | - |
| 6์ฐจ์‹œ | 2024.04.03 | ์ž๋ฃŒ๊ตฌ์กฐ | [๋‚˜๋งŒ ์•ˆ๋˜๋Š” ์—ฐ์• ](https://www.acmicpc.net/problem/14621) | - |
| 7์ฐจ์‹œ | 2024.04.04 | ์ €์šธ | [์ €์šธ](https://www.acmicpc.net/problem/2437) | - |
| 10์ฐจ์‹œ| 2024.04.29 | ๋ฐฑํŠธ๋ž˜ํ‚น | [๋น„์ˆ](https://www.acmicpc.net/problem/1799) | - |
| 10์ฐจ์‹œ | 2024.04.29 | ๋ฐฑํŠธ๋ž˜ํ‚น | [๋น„์ˆ](https://www.acmicpc.net/problem/1799) | - |
| 16์ฐจ์‹œ | 2024.06.04 | ๋ณ„์ž๋ฆฌ ๋งŒ๋“ค๊ธฐ | [๋ณ„์ž๋ฆฌ ๋งŒ๋“ค๊ธฐ](https://www.acmicpc.net/problem/4386) | - |
---
83 changes: 83 additions & 0 deletions yuyu0830/์ž๋ฃŒ๊ตฌ์กฐ/4386.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#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;
}
}

}