Pagini recente » Cod sursa (job #2963724) | Cod sursa (job #2383450) | Cod sursa (job #2334384) | Cod sursa (job #3189128) | Cod sursa (job #812137)
Cod sursa(job #812137)
#include <fstream>
#include <algorithm>
using namespace std;
#define nmax 200000
#define mmax 400000
ifstream f("apm.in");
ofstream g("apm.out");
int n,m,t[nmax],cost_maxim,nr,r1,r2,vecx[mmax],vecy[mmax];
struct muchie
{
int x,y;
int cost;
}v[mmax];
bool cmp(muchie a,muchie b)
{
return a.cost<b.cost;
}
int rad(int x)
{
if (t[x]==0)
return x;
else
return rad(t[x]);
}
void citire()
{
f>>n>>m;
for (int i=1;i<=m;++i)
f>>v[i].x>>v[i].y>>v[i].cost;
}
int main()
{
citire();
sort(v+1,v+m+1,cmp);
int i=1;
while (nr!=n-1)
{
r1=rad(v[i].x);
r2=rad(v[i].y);
if (r1!=r2)
{
cost_maxim+=v[i].cost;
nr++;
t[r1]=r2;
vecx[i]=v[i].x;
vecy[i]=v[i].y;
}
i++;
}
g<<cost_maxim<<'\n'<<n-1<<'\n';
for (i=1;i<=m;++i)
g<<vecx[i]<<" "<<vecy[i]<<'\n';
return 0;
}