Pagini recente » Cod sursa (job #1270311) | Cod sursa (job #3038854) | Cod sursa (job #1589421) | Cod sursa (job #962158) | Cod sursa (job #1097212)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
struct muchie{ int x, y, c;}a[400002];
struct cmp
{
bool operator()(const muchie &a, const muchie &b)const
{
if (a.c<b.c) return 1;
return 0;
}
};
int N, M, GR[200002], K, cost, sol[200002];
int grupa(int nod)
{
if (GR[nod]==nod) return nod;
GR[nod]=grupa(GR[nod]);
return GR[nod];
}
void uneste(int x, int y)
{
GR[grupa(x)]=grupa(y);
}
int main()
{
f>>N>>M;
for (int i=1; i<=M; ++i)
f>>a[i].x>>a[i].y>>a[i].c;
sort(a+1, a+M+1, cmp());
for (int i=1; i<=N; ++i)
GR[i]=i;
for (int i=1; i<=M && K!=N-1; ++i)
{
if ( grupa(a[i].x) != grupa(a[i].y) )
{
cost+=a[i].c; uneste(a[i].x, a[i].y);
sol[++K]=i;
}
}
g<<cost<<'\n'<<K<<'\n';
for (int i=1; i<=K; ++i)
g<<a[sol[i]].x<<' '<<a[sol[i]].y<<'\n';
return 0;
}