Pagini recente » Cod sursa (job #616359) | Cod sursa (job #1118451) | Cod sursa (job #3039809) | Cod sursa (job #371109) | Cod sursa (job #2039201)
#include <iostream>
#include <algorithm>
#include <fstream>
#define DM 200005
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie { int x, y, cost;};
muchie v[DM], arb[DM];
int n, m, cost_total, dad[DM], a, b, c, dim;
bool cmp(muchie A, muchie B){
return A.cost < B.cost;
}
int root(int nod){
if(dad[nod] == nod)
return nod;
return dad[nod] = root(dad[nod]);
}
void unite(int x, int y){
dad[root(x)] = root(y);
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++){
fin >> a >> b >> c;
v[i] = {a, b, c};
}
for(int i = 1; i <= n; i++){
dad[i] = i;
}
sort(v + 1, v + m + 1, cmp);
for(int i = 1; i <= m; i++){
int x = v[i].x;
int y = v[i].y;
int c = v[i].cost;
if(root(x) != root(y)){
unite(x, y);
arb[++dim] = {x, y, c}; // nu ne pasa de cost
cost_total += c;
}
}
fout << cost_total << '\n';
fout << dim << '\n';
for(int i = 1; i <= dim; i++)
fout << arb[i].x << ' ' << arb[i].y << '\n';
return 0;
}