← Back to List

1197번: 최소 스패닝 트리 ↗

Solutions

C++14
1.6 KB | 1572 chars
#include <bits/stdc++.h>
using namespace std;


template <class T>  struct MinimumSpanningTree {
  /*
    T: 가중치의 타입

    n: 노드 개수
    m: 간선 개수
    result : MST 결과 (가중치 합)
  */ 
  struct Edge { 
    int u, v;
    T weight;

    Edge(int u1, int v1, T weight1) : u(u1), v(v1), weight(weight1) {}
    bool operator< (Edge other) const { return weight < other.weight; }
  };

  int n, m;
  vector<int> uf;
  vector<Edge> edges;
  vector<Edge> chosen_edges;

  T result; // MST의 가중치 합
  int cnt; // 뽑은 간선 수

  MinimumSpanningTree(int n1, int m1) : n(n1), m(m1) {
    uf.resize(n + 1, -1);
    result = 0;
    cnt = 0;
  }

  int find(int a) {
    /*
      Union-Find: Find 연산
    */
    if (uf[a] < 0) return a;
    return uf[a] = find(uf[a]);
  }

  int merge(int a, int b) {
    /*
      Union-Find: Union
      합쳐진 경우 true 반환
    */

    a = find(a);
    b = find(b);

    if(a == b) return false;

    uf[b] = a;
    return true;
  }

  void add_edge(int u, int v, T cost) {
    edges.push_back(Edge(u, v, cost));
  }

  void run() {
    sort(edges.begin(), edges.end());

    for(int i = 0; ; i++) {
      if(merge(edges[i].u,  edges[i].v)) {
        result += edges[i].weight;

        chosen_edges.push_back(edges[i]);
        if(++cnt == n - 1) break;
      }
    }
  }
};


int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);cout.tie(NULL);

  int V, E;

  cin >> V >> E;

  MinimumSpanningTree <int> mst(V, E);

  for(int i = 0; i < E; i++) {
    int a, b, c;
    cin >> a >> b >> c;

    mst.add_edge(a, b, c);
  }

  mst.run();

  cout << mst.result;


  

}