Cod sursa(job #1131503)

Utilizator FasinedJohnny Bravo Fasined Data 28 februarie 2014 20:38:45
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include<cstdio>
#include<algorithm>
using namespace std;
struct gr{
    int x;
    int y;
    int c;
}a[400001];
int n,m,i,j,k,s,t1,t2,t,x,viz[200001],sol[200001];


bool cmp(const gr a1,const gr a2)
{
    return a1.c<a2.c;
}

void re(int x)
{
    t=x;
    while (viz[t]!=0)t=viz[t];
}

void citire()
{
    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",&a[i].x,&a[i].y,&a[i].c);
}

int main()
{
    citire();
    sort(a+1,a+m+1,cmp);
    i=0;
    while (k<n-1){
        i++;
        re(a[i].x);t1=t;
        re(a[i].y);t2=t;
        if(t1!=t2){
            k++;
            s=s+a[i].c;
            sol[k]=i;
            if(t1<t2)viz[t1]=t2;
            else viz[t2]=t1;
        }
    }
    printf("%d\n%d\n",s,k);
    for(i=1;i<=k;i++)printf("%d %d\n",a[sol[i]].x,a[sol[i]].y);
    return 0;
}