Pagini recente » Cod sursa (job #2282544) | Cod sursa (job #1309660) | Cod sursa (job #463056) | Cod sursa (job #1385214) | Cod sursa (job #2394850)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int Nmax=400000;
struct muchie
{
int x,y,c;
};
muchie v[Nmax+5];
muchie sol[(Nmax>>1)+5];
int cost,k;
int main()
{
int arb[(Nmax>>1)+5]= {0},n,m;
fin>>n>>m;
int nr=0;
while(m--)
{
nr++;
fin>>v[nr].x>>v[nr].y>>v[nr].c;
}
for(int i=1; i<=nr-1; i++)
for(int j=i+1; j<=nr; j++)
if(v[i].c>v[j].c)
swap(v[i],v[j]);
//for(int i=1; i<=nr; i++)cout<<v[i].x<<" "<<v[i].y<<" "<<v[i].c<<"\n";
for(int i=1; i<=n; i++)
arb[i]=i;
int nrsel=0;
k=1;
do
{
// if(nrsel==n-1)break;
if(arb[v[k].x]!=arb[v[k].y])
{
int c,b;
nrsel++;
cost+=v[k].c;
sol[nrsel].x=v[k].x;
sol[nrsel].y=v[k].y;
c=max(arb[v[k].x],arb[v[k].y]);
b=min(arb[v[k].x],arb[v[k].y]);
for(int j=1; j<=n; j++)
if(arb[j]==b)
arb[j]=c;
}
k++;
}
while(nrsel!=n-1);
fout<<cost<<"\n"<<nrsel<<"\n";
for(int i=1; i<=nrsel; i++)
fout<<sol[i].x<<" "<<sol[i].y<<"\n";
}