| 12
 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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 
 | #include<iostream>#include<cstring>
 #include<cstdio>
 #include<cmath>
 #include<algorithm>
 #include<queue>
 #define ll long long
 #define INF 0x7fffffff
 #define re register
 
 using namespace std;
 
 int read()
 {
 register int x = 0,f = 1;register char ch;
 ch = getchar();
 while(ch > ‘9‘ || ch < ‘0‘){if(ch == ‘-‘) f = -f;ch = getchar();}
 while(ch <= ‘9‘ && ch >= ‘0‘){x = x * 10 + ch - 48;ch = getchar();}
 return x * f;
 }
 
 struct edge{
 int x,y,z;
 }a[500005];
 
 struct EDGE{
 int next,to,x,save;
 }e[500005];
 
 struct node{
 int k,dis;
 bool operator < (const node & x) const {return x.dis < dis;}
 }now;
 
 priority_queue <node> que;
 
 int cnt,d[100005];
 
 void add(int x,int y,int a)
 {
 e[++cnt].to = y;
 e[cnt].x = a;
 e[cnt].next = d[x];
 d[x] = cnt;
 }
 
 int n,m,q,x,y,z,ans,minn[100005],vis[100005];
 
 int mysort(edge a1, edge a2)
 {
 if(a1.x != a2.x) return a1.x < a2.x;
 if(a1.y != a2.y) return a1.y < a2.y;
 return a1.z < a2.z;
 }
 
 int main()
 {
 n = read();
 m = read();
 for(re int i = 1; i <= m; i++)
 {
 a[i].x = read(); a[i].y = read(); a[i].z = read();
 }
 sort(a + 1, a + m + 1, mysort);
 for(re int i = 1; i <= m; i++)
 if(a[i].x != a[i - 1].x || a[i].y != a[i - 1].y)
 {
 add(a[i].x, a[i].y, a[i].z);
 add(a[i].y, a[i].x, a[i].z);
 }
 for(re int i = 1; i <= n; i++) minn[i] = INF;
 que.push((node){1,0});
 while(!que.empty())
 {
 now = que.top();
 que.pop();
 vis[now.k] = 1;
 for(re int i = d[now.k]; i; i = e[i].next)
 if(!vis[e[i].to] && e[i].x < minn[e[i].to])
 {
 minn[e[i].to] = e[i].x;
 que.push((node){e[i].to,minn[e[i].to]});
 }
 }
 for(re int i = 2; i <= n; i++) ans = ans + minn[i];
 printf("%d\n",ans);
 return 0;
 }
 
 |