최소 신장 트리 문제
- 크루스칼 알고리즘 이용
- 바킹독 최소 신장 트리 참고했음
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool compare(vector<int> &a, const vector<int> &b) {
if (a[2] > b[2]) {
return false;
} else {
return true;
}
}
int find(vector<int> &p, int x) {
if (p[x] < 0) return x;
return p[x] = find(p, p[x]);
}
bool is_diff_group(vector<int> &p, int x, int y) {
x = find(p, x); y = find(p, y);
if (x == y) return false;
if (p[x] > p[y]) swap(x, y);
p[x] += p[y];
p[y] = x;
return true;
}
int solution(int n, vector<vector<int>> costs) {
int answer = 0;
int count = 0;
sort(costs.begin(), costs.end(), compare);
vector<int> p(n, -1);
for (int i=0; i<costs.size(); i++) {
if (!is_diff_group(p, costs[i][0], costs[i][1])) continue;
answer += costs[i][2];
count++;
if (count == n-1) break;
}
return answer;
}
Comments powered by Disqus.