Skip to content

Commit 0912033

Browse files
committedOct 20, 2023
boj 6497 전력난
1 parent 061572b commit 0912033

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
 
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#define MAX 200001
5+
using namespace std;
6+
7+
typedef struct Node {
8+
int u, v, w;
9+
}Node;
10+
11+
Node list[MAX];
12+
int parent[MAX];
13+
int N, M, sum;
14+
15+
bool cmp(Node a, Node b) {
16+
return a.w < b.w;
17+
}
18+
19+
void init() {
20+
for (int i = 0; i < N; i++) {
21+
parent[i] = i;
22+
}
23+
}
24+
25+
int find(int v) {
26+
if (parent[v] == v) return v;
27+
return parent[v] = find(parent[v]);
28+
}
29+
30+
bool unionNode(int u, int v) {
31+
u = find(u);
32+
v = find(v);
33+
34+
if (parent[u] != parent[v]) {
35+
parent[v] = parent[u];
36+
return true;
37+
}
38+
39+
return false;
40+
}
41+
42+
void func() {
43+
init();
44+
45+
sort(list, list + M, cmp);
46+
47+
int ret = 0;
48+
int cnt = 0;
49+
for (int i = 0; i < M; i++) {
50+
int u = list[i].u;
51+
int v = list[i].v;
52+
53+
if (unionNode(u, v)) {
54+
ret += list[i].w;
55+
cnt++;
56+
}
57+
58+
if (cnt == N - 1) break;
59+
}
60+
61+
cout << sum - ret << '\n';
62+
}
63+
64+
void input() {
65+
cin >> N >> M;
66+
if (!N && !M) exit(0);
67+
68+
sum = 0;
69+
for (int i = 0; i < M; i++) {
70+
cin >> list[i].u >> list[i].v >> list[i].w;
71+
sum += list[i].w;
72+
}
73+
}
74+
75+
int main() {
76+
cin.tie(NULL); cout.tie(NULL);
77+
ios::sync_with_stdio(false);
78+
79+
while (1) {
80+
input();
81+
func();
82+
}
83+
84+
return 0;
85+
}

0 commit comments

Comments
 (0)
Please sign in to comment.