Pagini recente » Cod sursa (job #2954307) | Cod sursa (job #1243678) | Cod sursa (job #1320286) | Cod sursa (job #1414252) | Cod sursa (job #1460961)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ofstream fout("apm.out");
ifstream fin("apm.in");
const int NMAX = 400005;
int n, m, cost, nr;
int Set[NMAX];
struct muchie { int x, y, c; };
vector<muchie> Arbore;
vector<muchie> v;
bool cmp(muchie a, muchie b) { return a.c < b.c; }
void citire()
{
int x, y, c;
fin >> n >> m;
for(int i=0; i<m; i++) {
fin >> x >> y >> c;
v.push_back({x,y,c});
}
for(int i=0; i<n; i++) Set[i] = i;
}
int find_set(int a)
{
while(Set[a] != a) a = Set[a];
return a;
}
void unite_set(int a, int b)
{
Set[find_set(a)] = find_set(b);
}
void afis()
{
fout << cost << '\n' << Arbore.size() << '\n';
for(int i=0; i<Arbore.size(); i++) fout << Arbore[i].y << ' ' << Arbore[i].x << '\n';
}
int main()
{
citire();
sort(v.begin(), v.end(), cmp);
for(int i=0; i<v.size(); i++) {
if( find_set(v[i].x) != find_set(v[i].y) )
{
unite_set(v[i].x, v[i].y);
Arbore.push_back({v[i].x, v[i].y, v[i].c});
cost += v[i].c;
}
}
afis();
return 0;
}