Pagini recente » Cod sursa (job #2319716) | Cod sursa (job #1026302) | Cod sursa (job #1261353) | Cod sursa (job #726597) | Cod sursa (job #1261374)
#include <fstream>
#include <algorithm>
#define MMAX 400001
#define NMAX 200001
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie
{
int x, y;
double cost;
};
muchie G[MMAX];
int n, m;
int APM[NMAX], conex[NMAX];
int costAPM;
void citire();
int compara(muchie a, muchie b);
void kruskal();
void afisare();
int main()
{
citire();
sort(G, G+m, compara);
/*for(i=0;i<m;++i)
fout<<G[i].x<<' '<<G[i].y<<' '<<G[i].cost<<'\n';*/
kruskal();
afisare();
return 0;
}
void citire()
{
int i;
fin>>n>>m;
for(i=0;i<m;++i)
{
fin>>G[i].x>>G[i].y>>G[i].cost;
}
for(i=1;i<=n;++i) conex[i]=i;
}
int compara(muchie a, muchie b)
{
return a.cost<b.cost;
}
void kruskal()
{
int nrm=0, i, j, minim, maxim;
for(i=0;nrm<n-1;++i)
{
if(conex[G[i].x]!=conex[G[i].y])
{
APM[++nrm]=i; //retin muchia
costAPM+=G[i].cost;
if(conex[G[i].x]>conex[G[i].y])
{
minim=conex[G[i].y];
maxim=conex[G[i].x];
}
else
{
minim=conex[G[i].x];
maxim=conex[G[i].y];
}
for(j=1;j<=n;++j)
if(conex[j]==maxim) conex[j]=minim;
}
}
}
void afisare()
{
int i;
fout<<costAPM<<'\n';
fout<<n-1<<'\n';
for(i=1;i<=n-1;++i)
{
fout<<G[APM[i]].x<<' '<<G[APM[i]].y<<'\n';
}
}