标签:return main using ++ int void style bre namespace
最小生成树
1 #include <iostream> 2 #include <algorithm> 3 #define N 50005 4 using namespace std; 5 6 int n, m; 7 struct Node{ 8 int x, y, val; 9 friend bool operator <(const Node &a, const Node &b){ 10 return a.val < b.val; 11 } 12 }node[N]; 13 14 int fa[N]; 15 int sum = 0; 16 int find(int x){ 17 return fa[x] == x? x: fa[x] = find(fa[x]); 18 } 19 int cnt = 0; 20 void kruskal(){ 21 for(int i = 0; i <= n; i++) 22 fa[i] = i; 23 for(int i = 0; i < m; i++){ 24 int ax = find(node[i].x); 25 int ay = find(node[i].y); 26 if(ax != ay){ 27 fa[ax] = ay; 28 sum += node[i].val; 29 cnt++; 30 } 31 if(cnt == n-1) 32 break; 33 } 34 } 35 36 37 int main(){ 38 cin >> n >> m; 39 for(int i = 0; i < m; i++){ 40 cin >> node[i].x >> node[i].y >> node[i].val; 41 } 42 sort(node, node+m); 43 kruskal(); 44 cout << sum << endl; 45 return 0; 46 }
标签:return main using ++ int void style bre namespace
原文地址:https://www.cnblogs.com/zllwxm123/p/10544513.html