[BOJ] 1922 네트워크 연결

Time Lapse :10min 20sec

1922.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
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int com[1001],n,m, ans;
struct conn{
int f, e, c;
};
bool operator<(conn a, conn b){
return a.c > b.c;
}
priority_queue<conn> pq;
int getG(int a) {
return a == com[a] ? a : com[a] = getG(com[a]);
}

void makeG(conn con) {
int ga = getG(con.f);
int gb = getG(con.e);
if(ga == gb) return;
ans += con.c;
com[ga] = com[gb] = min(ga, gb);
}
int main() {
scanf("%d %d",&n,&m);
for(int i = 1; i <= n; ++i)
com[i] = i;
for(int i = 0; i < m; ++i) {
conn con;
scanf("%d %d %d",&con.f, &con.e, &con.c);
pq.push(con);
}
while(!pq.empty()) {
makeG(pq.top());
pq.pop();
}
printf("%d",ans);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/31/PS/BOJ/1922/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.