Pagini recente » Cod sursa (job #433965) | Cod sursa (job #429258) | Cod sursa (job #1228350) | Cod sursa (job #977470) | Cod sursa (job #1101472)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int N, M, sol[200002], K, GR[200002], cost;
struct muchie{ int c, y, x; }a[400005];
struct cmp
{
bool operator()(const muchie &A, const muchie &B)const
{
return (A.c<B.c);
}
};
int grupa(int nod)
{
if (GR[nod]==nod) return nod;
GR[nod]=grupa(GR[nod]);
return GR[nod];
}
inline void unite(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))
{
sol[++K]=i; cost+=a[i].c;
unite(a[i].x, a[i].y);
}
g<<cost<<'\n'<<K<<'\n';
for (int i=1; i<=K; ++i)
g<<a[sol[i]].x<<' '<<a[sol[i]].y<<'\n';
return 0;
}