Cod sursa(job #567358)

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

void sortare ()
{
    int i,j;
    muchie aux;
    for(i=1;i<=m;i++)
        for(j=i+1;j<=m;j++)
            if(v[i].c>v[j].c)
            {
                aux=v[i];
                v[i]=v[j];
                v[j]=aux;
            }
}

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);
    sortare ();
    i=1;j=1;k=1;
    while(k<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=l[v[i].y];
            yy=l[v[i].x];
            for(j=1;j<=n;j++)
                if(l[j]==xx) l[j]=yy;
        }
        i++;
    }
    printf("%d", ct);
    for(i=1;i<=n-1;i++) printf("%d %d\n", w[i].a, w[i].b);
    return 0;
}