-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1197.cpp
More file actions
58 lines (57 loc) · 922 Bytes
/
1197.cpp
File metadata and controls
58 lines (57 loc) · 922 Bytes
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
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
struct e{
int x,y,c;
bool operator < (const e &tmp) const{
return c<tmp.c;
}
};
int p[10001];
int Find(int x)
{
if(x==p[x])
return x;
else{
return p[x]=Find(p[x]);
}
}
void Union(int a,int b)
{
a=Find(a);
b=Find(b);
p[b]=a;
}
vector<struct e> vec;
int main()
{
int v,e,x,y,cost;
cin>>v>>e;
for(int i=1;i<=v;i++)
{
p[i]=i;
}
int ans=0;
for(int i=0;i<e;i++)
{
cin>>x>>y>>cost;
struct e tmp={x,y,cost};
vec.push_back(tmp);
}
sort(vec.begin(),vec.end());
for(int i=0;i<e;i++)
{
struct e tmp=vec[i];
int nx=Find(tmp.x);
int ny=Find(tmp.y);
if(nx!=ny)
{
Union(nx,ny);
ans+=tmp.c;
}
}
cout<<ans;
return 0;
}