Cod sursa(job #2348593)

Utilizator Robys01Robert Sorete Robys01 Data 19 februarie 2019 20:30:39
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <fstream>
#include <algorithm>
#define NMAX 200005
#define MMAX 400005
using namespace std;

ifstream cin("apm.in");
ofstream cout("apm.out");

struct Muchie{
    int e1, e2, cost;
};

int n, m, nr, s;
int Arb[NMAX], C[NMAX];
Muchie V[MMAX];

bool compara(Muchie a, Muchie b){
    return a.cost < b.cost;
}

void citire(){
    cin>>n>>m;
    for(int i=1; i<=m; i++){
        cin>>V[i].e1>>V[i].e2>>V[i].cost;
    }
    sort(V+1, V+m+1, compara);
    for(int i=1; i<=n; i++){
        Arb[i] = i;
    }
}


int main(){
    citire();

    for(int i=1; nr<n-1; i++){
        if(Arb[V[i].e1] != Arb[V[i].e2]){
            s+=V[i].cost;
            C[++nr] = i;

            int minim, maxim;
            minim = min(Arb[V[i].e1], Arb[V[i].e2]);
            maxim = max(Arb[V[i].e1], Arb[V[i].e2]);

            for(int j=1; j<=n; j++){
                if(Arb[j] == minim)
                    Arb[j] = maxim;
            }

        }

    }
    cout<<s<<'\n'<<nr<<'\n';
    for(int i=1; i<=n; i++){
        cout<<V[C[i]].e1<<' '<<V[C[i]].e2<<' '<<V[C[i]].cost<<'\n';
    }


    return 0;
}