forked from Dasona/BOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ_1647번.cpp
96 lines (64 loc) · 1.16 KB
/
BOJ_1647번.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//도시 분할 계획 1647번
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX_SIZE 100010
class Edge {
public:
int cost;
int v1;
int v2;
public:
Edge(int c, int a, int b) {
cost = c;
v1 = a;
v2 = b;
}
};
bool comp(Edge a, Edge b) {
if (a.cost != b.cost) return a.cost < b.cost;
else if (a.v1 != b.v1) return a.v1 < b.v1;
else return a.v2 < b.v2;
}
vector<Edge> vt;;
int parent[MAX_SIZE];
int n, m, sum;
int find(int n) {
if (parent[n] == n) return n;
else return parent[n] = find(parent[n]);
}
void Union(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
parent[a] = b;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int a, b, c;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
vt.push_back(Edge(c, a, b));
}
for (int i = 1; i <= n; i++) {
parent[i] = i;
}
stable_sort(vt.begin(), vt.end(), comp);
sum = 0;
int MAX = 0;
for (int i = 0; i < vt.size(); i++) {
int c = vt[i].cost;
int V1 = vt[i].v1;
int V2 = vt[i].v2;
if (find(V1) != find(V2)) {
Union(V1, V2);
sum += c;
MAX = c;
}
}
cout << sum - MAX;
return 0;
}