Pagini recente » Cod sursa (job #2379866) | Cod sursa (job #2171068) | Cod sursa (job #2648627) | Profil georgiana328 | Cod sursa (job #2867496)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f ("apm.in");
ofstream g ("apm.out");
int cost,P[200001],k,n,m;
struct muchie
{
int x,y,c;
};
muchie u[200001],sol[200001];
bool comp(muchie a,muchie b)
{
return a.c<b.c;
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;i++)
{
f>>u[i].x>>u[i].y>>u[i].c;
P[i]=i;
}
sort(u+1,u+n+1,comp);
int i=1;
while(k<n-1)
{
if(P[u[i].x]!=P[u[i].y])
{
++k;
sol[k]=u[i];
cost=cost+u[i].c;
int nr1=P[u[i].x], nr2=P[u[i].y];
for(int j=1;j<=n;j++)
if(P[j]==nr2)
P[j]=nr1;
}
++i;
}
g<<cost<<"\n"<<k<<"\n";
for(int i=1;i<=k;i++)
g<<u[i].x<<" "<<u[i].y<<"\n";
return 0;
}