Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstring>
- #include <string>
- #include <cmath>
- #include <cstdlib>
- #include <map>
- #include <set>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- struct edge {
- int from;
- int to;
- int weight;
- bool inline friend operator < (edge x, edge y)
- {
- if (x.weight < y.weight)
- return true;
- return false;
- }
- } edges[4500000];
- int parent[3001];
- int findSet(int x)
- {
- if (parent[x] == x)
- return x;
- return parent[x] = findSet(parent[x]);
- }
- int main()
- {
- int n,m;
- cin >> n >> m;
- for (int i=0;i<m;i++)
- {
- int f,t,w;
- cin >> f >> t >> w;
- edges[i].from = f;
- edges[i].to = t;
- edges[i].weight = w;
- }
- sort(edges , edges + m);
- for (int i=1;i<=n;i++)
- parent[i] = i;
- int cnt = n, cur = 0;
- int sum = 0;
- while (cnt != 1)
- {
- if (findSet(edges[cur].from) != findSet(edges[cur].to))
- {
- parent[findSet(edges[cur].to)] = findSet(edges[cur].from);
- sum += edges[cur].weight;
- cnt--;
- }
- cur++;
- }
- cout << sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement