-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp4.cpp
More file actions
45 lines (36 loc) · 659 Bytes
/
Copy pathp4.cpp
File metadata and controls
45 lines (36 loc) · 659 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
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,ans=0,cs=0;
int f[5001];
struct bian{
int x,y,z;
}b[200001];
bool cmp(bian a,bian b){
return a.z<b.z;
}
void csh(){
for(int i=0;i<5001;i++)
f[i]=i;
}
int getf(int x){
if(f[x]==x)return x;
return f[x]=getf(f[x]);
}
int main(){
csh();
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
scanf("%d%d%d",&b[i].x,&b[i].y,&b[i].z);
sort(b,b+m,cmp);
for(int i=0;i<m;i++){
if(cs==n-1)break;
if(getf(b[i].x)==getf(b[i].y))continue;
f[getf(b[i].x)]=getf(b[i].y);
cs++;
ans=ans+b[i].z;
}
if(cs<n-1)printf("Could not complete the task!\n");
else printf("%d\n",ans);
return 0;
}