Напишите программу ниже |
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct edge
{
int l,a,b;
};
bool cmp(const edge &a, const edge &b)
{
return a.l < b.l;
}
int main()
{
int m, n;
cin >> n >> m;
vector < edge > g(m);
for (int i = 0; i < m; i++)
{
int a, b, l;
cin >> a >> b >>l;
edge e;
e.l = l; e.a= a-1; e.b = b-1;
g[i] = e;
}
long long cost = 0;
vector < pair<int, int> > res;
sort(g.begin(), g.end(),cmp);
vector<int> tree_id(n);
for (int i = 0; i < n; ++i)
tree_id[i] = i;
for (int i = 0; i < m; ++i)
{
int a = g[i].a, b = g[i].b, l = g[i].l;
if (tree_id[a] != tree_id[b])
{
|
|
res.push_back(make_pair(a, b));
int old_id = tree_id[b], new_id = tree_id[a];
for (int j = 0; j < n; ++j)
if (tree_id[j] == old_id)
tree_id[j] = new_id;
}
}
cout << cost;
return 0;
}
|