-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1197.cpp
More file actions
63 lines (49 loc) · 1.09 KB
/
BOJ_1197.cpp
File metadata and controls
63 lines (49 loc) · 1.09 KB
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
//21 09 25
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<pair<int, pair<int, int>>> road;
int parent_node[10000+1];
int total_cost = 0;
int Find_parent(int node)
{
if (parent_node[node] == node)
return node;
parent_node[node] = Find_parent(parent_node[node]);
return parent_node[node];
}
void Make_MST(void)
{
for (auto iter = road.begin(); iter!=road.end(); iter++)
{
int node1 = (*iter).second.first;
int node2 = (*iter).second.second;
int parent1 = Find_parent(node1);
int parent2 = Find_parent(node2);
if (parent1 != parent2)
{
parent_node[parent2] = parent1;
total_cost += (*iter).first;
}
}
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int v, e;
cin>>v>>e;
while (e--)
{
int a, b, c;
cin>>a>>b>>c;
road.push_back({c, {a, b}});
}
for (int i=1; i<=v; i++)
parent_node[i] = i;
sort(road.begin(), road.end());
Make_MST();
cout<<total_cost;
return 0;
}