Cod sursa(job #567348)

Utilizator devill_08Buli.vlad devill_08 Data 29 martie 2011 22:43:34
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#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;
}