Pagini recente » Cod sursa (job #436098) | Cod sursa (job #1848499) | Cod sursa (job #2562750) | Cod sursa (job #2822380) | Cod sursa (job #2830152)
#include <fstream>
#include <vector>
#include <algorithm>
#define NMAX 200003
using namespace std;
int tati[NMAX+3],n,m;
int cost=0;
bool viz[400003];
struct val{
int x,y,cost;
}muchii[400003];
//vector<val> muchii;
ifstream fin ("apm.in");
ofstream fout ("apm.out");
int cmp(val a,val b)
{
return a.cost<b.cost;
}
int cRad(int nr)
{
int k=nr;
while(tati[k]>0)
{
k=tati[k];
}
return k;
}
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
int x,y,cost;
fin>>muchii[i].x>>muchii[i].y>>muchii[i].cost;
}
sort(muchii+1,muchii+m+1,cmp);
int nrm=0;
for(int i=1; nrm<n-1; i++)
{
int rad1=cRad(muchii[i].x);
int rad2=cRad(muchii[i].y);
if(rad1!=rad2)
{
cost+=muchii[i].cost;
viz[i]=1;
nrm++;
if(tati[rad1]<=tati[rad2])
{
tati[rad1]+=tati[rad2];
tati[rad2]=rad1;
}
else{
tati[rad2]+=tati[rad1];
tati[rad1]=rad2;
}
}
}
fout<<cost<<"\n"<<n-1<<"\n";
for(int i=1; i<=m; i++)
{
if(viz[i])
{
fout<<muchii[i].x<<" "<<muchii[i].y<<"\n";
}
}
return 0;
}