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
|
#include<bits/stdc++.h>
#define FOR(i,l,r) for(register int i=l;i<=r;++i)
#define Graph(i,u) for(register int i=head[u];i;i=e[i].next)
using namespace std;
#define pii pair<int,int>
#define int long long
const int N=5e3+5,M=2e5+5;
const int inf=2147483647;
struct Edge{
int u,v,w,next;
}e[M<<1];
int tot,head[M];
int n,m;
inline void addEdge(int u,int v,int w){
e[++tot].u=u;
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot;
}
bool vis[N];
int dis[N];
int ans,now=1,cnt;
priority_queue<pii,vector<pii>,greater<pii>>q;
inline void Prim(){
memset(dis,127,sizeof(dis));
dis[1]=0;
q.push(make_pair(0,1));
while(!q.empty()&&cnt<n){
// cout<<"QwQ";
int d = q.top().first,u = q.top().second;
q.pop();
if(vis[u])continue;
cnt++;
ans+=d;
vis[u]=1;
Graph(i,u){
if(e[i].w<dis[e[i].v]){
dis[e[i].v]=e[i].w;
q.push(make_pair(dis[e[i].v],e[i].v));
}
}
}
return;
}
signed main(){
cin>>n>>m;
FOR(i,1,m){
int x,y,z;
cin>>x>>y>>z;
addEdge(x,y,z);
addEdge(y,x,z);
}
Prim();
if(cnt==n){
cout<<ans<<endl;
}
else{
cout<<"orz"<<endl;
}
return 0;
}
|