Cod sursa(job #2591751)

Utilizator chriss_b_001Cristian Benghe chriss_b_001 Data 31 martie 2020 10:58:55
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#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];
int CC[200001];   /// vect de tati al comp conexe
int NrM[200001];  /// vect nr de ord 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++)
    {
        int k = NrM[i];
        g << M[k].i << ' ' << M[k].j << '\n';
    }
}

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

int grupa(int i) ///cautare cu compresie drum
{
    if(CC[i] == 0)
        return i;
    return CC[i] = grupa(CC[i]);
}

void Kruskal()
{
    sort(M + 1, M + m + 1, comp);
    int nm = 0; ///nr muchii ale APM
    for(int i = 1; i <= m; i++)
    {
        int ci = grupa(M[i].i);
        int cj = grupa(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;
}