Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 데이터베이스 배움터
- UnrealMP
- 백준 1253번
- command not found
- FBX
- Unreal
- 민겸수
- Linux
- hackerank
- 백준
- 1967번
- 1253번
- 언리얼 커스텀 플러그인
- UActor
- 5639
- objtofbx
- 셰그먼트트리
- 비재귀셰그먼트
- oracle
- 언리얼 플러그인
- 1759번
- 2단계로킹
- Security
- 오손데이터읽기
- SQL
- 의미와 무의미의 경계에서
- 트랜잭션 관리
- OS
- C++
- 실습
Archives
- Today
- Total
fatalite
별자리 만들기 - 4386번 백준 본문
문제
문제 난이도 : 골드 3
문제 분류 : 최소 스패닝 트리(Minimum Spanning Tree)
문제 리뷰
약간의 기하(초등)와 MST가 합쳐진 문제다.
double 자료형에 precision을 소수점 두자리까지 출력해야해서 골드 3으로 책정된 듯 하다.
문제 소스코드
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <unordered_set>
#include <memory.h>
using namespace std;
vector<vector<double>> Edges;
int Root[1001];
int n;
void Init()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
std::cout.tie(NULL);
}
int Find(int x) {
if (Root[x] == x) {
return x;
}
else {
return Root[x] = Find(Root[x]);
}
}
void Union(int x, int y) {
x = Find(x);
y = Find(y);
if (x != y) {
Root[x] = y;
}
}
vector<vector<double>> Kruskal() {
vector<vector<double>> MST;
for (int i = 0; i < Edges.size(); i++) {
vector<double> tmp = Edges[i];
if (Find(tmp[1]) == Find(tmp[2])) continue;
MST.push_back(tmp);
if (MST.size() == n - 1) return MST;
Union(tmp[1], tmp[2]);
}
}
int main()
{
//Initialize
Init();
cin >> n;
for (int i = 0; i < 1001; i++) {
Root[i] = i;
}
vector<pair<double, double>> StarPoint;
for (int i = 0; i < n; ++i) {
float x, y;
cin >> x >> y;
StarPoint.push_back({ x,y });
}
for (int i = 0; i < StarPoint.size(); i++) {
for (int j = 0; j < StarPoint.size(); j++) {
if (i != j) {
Edges.push_back({ sqrt(pow(StarPoint[i].first - StarPoint[j].first ,2)
+ pow(StarPoint[i].second - StarPoint[j].second ,2)) , double(i), double(j)});
}
}
}
sort(Edges.begin(), Edges.end(), less<vector<double>>());
double Length = 0;
vector<vector<double>> tmp = Kruskal();
for (vector<double> v : tmp) {
Length += v[0];
}
cout.precision(3);
cout << Length;
}
'코딩 인터뷰 > Graph Basic' 카테고리의 다른 글
줄 세우기 - 2252번 백준 (1) | 2023.10.03 |
---|---|
네트워크 연결 - 1922번 백준 (0) | 2023.09.26 |
녹색 옷 입은 애가 젤다지? - 4485번 백준 (0) | 2023.09.25 |
알고스팟 - 1261번 백준 (0) | 2023.09.25 |
미로 찾기 - 2178번 백준 (0) | 2023.09.16 |