Cod sursa(job #1922280)

Utilizator AntoniooMacovei Antonio Antonioo Data 10 martie 2017 16:48:49
Problema Arbore partial de cost minim Scor 70
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;
} mch[400001];
int k[200001], alese[200001];
bool cmp(muchie a, muchie b) {
    return a.c < b.c;
}
int main()
{
    freopen("apm.in","r",stdin);
    freopen("apm.out","w",stdout);
    int n, m, i, j, ct = 0, x, y, ia = 1, cy, t;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++) {
        scanf("%d%d%d",&mch[i].x,&mch[i].y,&mch[i].c);
    }
    sort(mch+1, mch+m+1, cmp);
    for(i=1;i<=n;i++)
        k[i] = i;
    for(j=1; ia <n; j++) {
        x = mch[j].x; y = mch[j].y;
        if(k[x] != k[y]) {
            alese[ia++] = j;
            ct += mch[j].c;
            cy = k[y];
            for(t = 1; t <= n; t++)
                if(k[t] == cy)
                    k[t] = k[x];
        }
    }
    printf("%d\n%d\n",ct,ia-1);
    for(i=1; i<ia; i++) {
        printf("%d %d\n",mch[alese[i]].y,mch[alese[i]].x);
    }
    return 0;
}