Pagini recente » Cod sursa (job #250656) | Cod sursa (job #2741434) | Cod sursa (job #2028321) | Cod sursa (job #1261381)
#include <fstream>
#include <algorithm>
#define nmax 200001
#define mmax 400001
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct muchie
{
int x, y;
double cost;
};
muchie Graf[mmax];
int APM[nmax]; // retin indicii celor n-1 muchii selectate
long double costAPM;
int conex[nmax]; // pt evidenta componentelor conexe
int m,n;
void citire();
int compara(muchie, muchie);
int main()
{
int i,j,k,l,minim,maxim;
citire();
sort(Graf, Graf+m, compara);
/*for(i=0; i<m; i++)
{
fout<<Graf[i].x<<' '<<Graf[i].y<<' '<<Graf[i].cost;
fout<<'\n';
}*/
j=0,l=1;
for (i=1; i<=n-1; i++)
{
for (; j<m; j++)
if(conex[Graf[j].x]!=conex[Graf[j].y])
{
if(conex[Graf[j].x]>conex[Graf[j].y])
{
minim=conex[Graf[j].y];
maxim=conex[Graf[j].x];
for(k=1; k<=n; k++)
if(conex[k]==maxim) conex[k]=minim;
}
else
{
minim=conex[Graf[j].x];
maxim=conex[Graf[j].y];
for(k=1; k<=n; k++)
if(conex[k]==maxim) conex[k]=minim;
}
APM[l++]=j;
}
}
for(l=1; l<=n-1; l++)
costAPM+=Graf[APM[l]].cost;
fout<<costAPM<<'\n'<<n-1<<'\n';
for(l=1; l<=n-1; l++)
{
fout<<Graf[APM[l]].y<<' '<<Graf[APM[l]].x;
fout<<'\n';
}
return 0;
}
void citire()
{
int i;
fin>>n>>m;
for(i=0; i<m; i++)
{
fin>>Graf[i].x>>Graf[i].y>>Graf[i].cost;
}
for(i=1; i<=n; i++) conex[i]=i; // la momentul initial fiecare varf apartine propriei comp conexe
}
int compara(muchie a, muchie b)
{
return a.cost<b.cost;
}