Cod sursa(job #1839792)

Utilizator miha1000Dica Mihai miha1000 Data 3 ianuarie 2017 14:24:11
Problema Arbore partial de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

struct muchie{
    int x,y,c;
};

muchie v[400000];


bool viz[200000];

bool comp(muchie a, muchie b){
    return (a.c<b.c);
}

int main()
{
    int n,m,i,j,x,y,lungime;
    ifstream f("apm.in");
    ofstream g("apm.out");
    f >> n >> m;
    lungime=0;
    for (i=1;i<=m;i++){
        f >> v[i].x >> v[i].y>> v[i].c;
    }
    n=0;
    sort(v+1,v+m+1,comp);
    viz[v[1].x]=1;

    for(i=1;i<=m;i++){
        x=v[i].x;
        y=v[i].y;
        if (viz[x]!=viz[y]){
            n++;
            lungime=lungime+v[i].c;
            viz[x]=1;
            viz[y]=1;
            v[n].x=x;
            v[n].y=y;
        }
    }
    g << lungime << "\n";
    g << n << "\n";
    for (i=1;i<=n;i++){
        g << v[i].x << " " << v[i].y << "\n";

    }
}