Pagini recente » Cod sursa (job #2468365) | Cod sursa (job #56903) | Cod sursa (job #679603) | Cod sursa (job #255506) | Cod sursa (job #2166065)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct muchie
{
int i, j, c;
};
int n, m, cost;
muchie M[400001];
int CC[200001], NrM[200000];
void citire()
{
f >> n >> m;
for(int i = 1; i <= m; i++)
f >> M[i].i >> M[i].j >> M[i].c;
}
void afis()
{
g << cost << '\n';
g << n - 1 << '\n';
for(int i = 1; i < n; i++)
g << M[NrM[i]].i << ' ' << M[NrM[i]].j << '\n';
}
bool comp(const muchie &a, const muchie &b)
{
return a.c < b.c;
}
int compcx(int i)
{
if(CC[i] == i)
return i;
CC[i] = compcx(CC[i]);
return CC[i];
}
void Kruskal()
{
sort(M + 1, M + m + 1, comp);
for(int i = 1; i <= n; i++)
CC[i] = i;
int nm = 0;
for(int i = 1; i <= m; i++)
{
int ci = compcx(M[i].i);
int cj = compcx(M[i].j);
if(ci != cj)
{
cost += M[i].c;
CC[ci] = cj; //reuniune cc
NrM[++nm] = i;
if(nm == n - 1)break;
}
}
}
int main()
{
citire();
Kruskal();
afis();
return 0;
}