Pagini recente » Cod sursa (job #2801340) | Cod sursa (job #336004) | Cod sursa (job #1517649) | Cod sursa (job #1521317) | Cod sursa (job #1075232)
//horatiu11
# include <cstdio>
# include <algorithm>
# define mmax 400001
# define nmax 200001
using namespace std;
int n,m,componenta[nmax],k,cost;
struct muchie{int x,y,c;}u[mmax],sol[mmax];
inline bool cmp(muchie a,muchie b)
{
return (a.c<b.c);
}
inline int father(int x)
{
if(componenta[x]!=x)componenta[x]=father(componenta[x]);
return componenta[x];
}
inline void unific(int t1, int t2)
{
componenta[t1]=t2;
}
inline void apm()
{
int i=1,nod1,nod2;
k=cost=0;
while(k<n-1)
{
nod1=u[i].x;nod2=u[i].y;
if(father(nod1)!=father(nod2))
{
sol[++k]=u[i];
cost+=u[i].c;
unific(father(nod1),father(nod2));
}
++i;
}
}
int main()
{
int i;
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=m;++i)
{
scanf("%d%d%d",&u[i].x,&u[i].y,&u[i].c);
componenta[i]=i;
}
sort(u+1,u+m+1,cmp);
apm();
printf("%d\n%d\n",cost,k);
for(i=1;i<=k;++i)
printf("%d %d\n",sol[i].x,sol[i].y);
return 0;
}