Pagini recente » Cod sursa (job #2308619) | Cod sursa (job #942826) | Cod sursa (job #378175) | Poze preONI 2007 - wii-play | Cod sursa (job #567348)
Cod sursa(job #567348)
#include <stdio.h>
#include <algorithm>
using namespace std;
struct muchie{
int x,y,c;
};
muchie v[100];
struct muchii{
int a,b;
};
muchii w[100];
int n,i,j,m,l[100],ct,xx,yy,k;
int cmp (muchie a, muchie b)
{
if(a.c<b.c) return a.c;
else return b.c;
}
void initializare ()
{
for(i=1;i<=n;i++) l[i]=i;
}
int main ()
{
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", &v[i].x, &v[i].y, &v[i].c);
sort(v+1,v+n+1,cmp);
while(j<n-1)
{
if(l[v[i].x]!=l[v[i].y])
{
k++;
ct=ct+v[i].c;
w[i].a=v[i].x; w[i].b=v[i].y;
xx=v[i].x;
yy=v[i].y;
for(k=1;k<=n;k++)
if(l[k]==xx) l[k]=yy;
}
i++;
}
printf("%d", ct);
for(i=1;i<=n;i++) printf("%d %d\n", w[i].a, w[i].b);
return 0;
}