본문 바로가기

Algorithm_BOJ(백준)/최소신장트리(MST)

[백준 4386 c++ O] 별자리 만들기

728x90
반응형

- 풀이 링크:

https://github.com/xhaktmchl/Algorithm_study/blob/main/BOJ/MST(%EC%B5%9C%EC%86%8C%EC%8B%A0%EC%9E%A5%ED%8A%B8%EB%A6%AC)/%5B%EB%B0%B1%EC%A4%80%204386%20c%2B%2B%20O%5D%20%EB%B3%84%EC%9E%90%EB%A6%AC%20%EB%A7%8C%EB%93%A4%EA%B8%B0.cpp 

 

GitHub - xhaktmchl/Algorithm_study: 알고리즘 이론 및 문제풀이

알고리즘 이론 및 문제풀이. Contribute to xhaktmchl/Algorithm_study development by creating an account on GitHub.

github.com

#include <iostream>
#include <algorithm> // fill_n, min,max, swap
//#include <map> // 중복 x 
//#include <string> // getline
//#include <cstring> // memset, strok, strstr
//#include <vector>
//#include <queue> // priority_queue<자료형, 구현체, 비교 연산자> (중복허용)
//#include <set> // 트리, 중복 x , multiset
//#include <unordered_set>
#include <cmath>
using namespace std;
/*
[백준 4386 c++ O] 별자리 만들기
문제: 
접근1: 모든 그래프 연결 최소비용 -> MST
시간복잡도: O(n*n/2)
풀이1: MST-크루스칼 -> 간선 구조체 배열 정의
    //1.입력
    //2.좌표를 이용해 간선 생성 후 배열에 저장
    //3.MST-크루스칼(유니온 파인드)
    //1) 부모배열 초기화
    //2)정렬: 간선배열 비용 오름차순
    //3) 유니온 파인드
    //파인드: 같은 집합 아니면
    //유니온: 합집합
풀이2: 풀이1과 같은데 MST-크루스칼 -> 간선 배열을 벡터로 정의, vector<pair<int, pair<int,int>>>
*/
struct ed {
    int n1, n2;
    double dist;
    
    bool operator<(ed& ref) {
        return this->dist < ref.dist;
    }
}Ed[10005];

int n;
double loc[105][3];
int cnt = 0;
int par[105];
double sum = 0;

void init() {
    for (int i = 1; i <= n; i++) par[i] = i;
}

int fin(int node) {
    if (par[node] == node) return node;
    else return par[node] = fin(par[node]); //경로 압축: 시간 단축
}

void uni(int a, int b) {
    int rootA = fin(a);
    int rootB = fin(b);

    if (rootA < rootB) par[rootB] = rootA;
    else par[rootA] = rootB;
}


int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(0);
    //1.입력
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> loc[i][0] >> loc[i][1];
    }
    //2.좌표를 이용해 간선 생성 후 배열에 저장
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            Ed[cnt].n1 = i; Ed[cnt].n2 = j;
            Ed[cnt++].dist = sqrt( pow(abs(loc[i][0] - loc[j][0]),2) + pow(abs(loc[i][1] - loc[j][1]),2) );
        }
    }
    //3.MST-크루스칼(유니온 파인드)
    //1) 부모배열 초기화
    init();
    //2)정렬: 간선배열 비용 오름차순
    sort(Ed, Ed + cnt);
    //3) 유니온 파인드
    //파인드: 같은 집합 아니면
    //유니온: 합집합
    for (int i = 0; i < cnt; i++) {
        int n1 = Ed[i].n1, n2 = Ed[i].n2;
        double dist = Ed[i].dist;
        if (fin(n1) != fin(n2)) {
            uni(n1, n2);
            sum += dist;
        }
    }
    cout << fixed; cout.precision(2); // cout 소수점 고정
    cout << sum << '\n';
    return 0;
}
반응형