Cod sursa(job #2641425)

Utilizator AndreiAlexandru2k3Ciucan Andrei Alexandru AndreiAlexandru2k3 Data 11 august 2020 13:15:44
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

ifstream f("apm.in");
ofstream g("apm.out");

struct muchie
{
    int i, j, c;
};
int n, m,
    cost;

muchie M[400001];///100*99/2+1
int CC[200001]; ///vectorul; de tati al comp conexe(multimi disjuncte)
int NrM[200000];  ///vectorul numerelor de ordine ale muchiilor alese in APM

void citire()
{
    f >> n >> m;
    for(int i = 1; i <= m; i++)
        f >> M[i].i >> M[i].j >> M[i].c;
}

void afis()
{
    g << cost << '\n';
    g << n - 1 << '\n';
    for(int i = 1; i < n; i++)
        g << M[NrM[i]].i << ' ' << M[NrM[i]].j << '\n';
}

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

int Find(int i)
{
    if(CC[i] == 0)
        return i;
    return CC[i] = Find(CC[i]);
}

void Kruskal()
{
    sort(M + 1, M + m + 1, comp);
    int nm = 0; ///nr de muchii ale APM
    for(int i = 1; i <= m; i++)
    {
        int ci = Find(M[i].i);
        int cj = Find(M[i].j);
        if(ci != cj)
        {
            cost += M[i].c;
            CC[ci] = cj;
            NrM[++nm] = i;
            if(nm == n - 1)
                break;
        }
    }
}

int main()
{
    citire();
    Kruskal();
    afis();
    return 0;
}