-
Notifications
You must be signed in to change notification settings - Fork 12
/
1774.cpp
64 lines (57 loc) · 982 Bytes
/
1774.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
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, pair<int,int> > iii;
vector<int> ps;
void init(int n)
{
ps.assign(n,0);
for (int i = 0 ; i < n; i++)
ps[i] = i; //vertice i na componente i
}
int findSet(int i)
{
return (ps[i] == i? i : ps[i] = findSet(ps[i]));
}
bool isSameSet(int i, int j)
{
return findSet(i) == findSet(j);
}
void unionSet(int i, int j)
{
ps[findSet(i)] = findSet(j);
}
int main()
{
vector< iii >adj;
int n;
int m;
int v;
int u;
int peso;
int min_cost;
while(cin >> n >> m)
{
for (int i = 0 ; i < m ; i++)
{
cin >> u >> v >> peso;
--u;
--v;
adj.push_back(iii(peso, pair<int,int> (u,v)));
}
min_cost = 0;
sort(adj.begin(), adj.end());
init(n);
for (int i = 0 ; i < m ; i++)
{
iii f = adj[i];
if (!isSameSet(f.second.first, f.second.second))
{
unionSet(f.second.first, f.second.second);
min_cost+= f.first;
}
}
cout << min_cost << '\n';
ps.clear();
adj.clear();
}
}