Cod sursa(job #2679471)

Utilizator DiagrDiana Grigore Diagr Data 30 noiembrie 2020 17:11:16
Problema Arbore partial de cost minim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include<vector>

using namespace std;
int r[200001], n, m;
vector<pair<pair<int,int>, int>> graf, apcm;
ifstream f("apm.in");
ofstream g("apm.out");

bool cmp (pair<pair<int,int>, int> a, pair<pair<int,int>, int> b)
{
    if (a.second < b.second)
        return 1;
    else
        return 0;

}

void initializare (int u)
{
    r[u] = u;
}

int reprezentare (int u)
{
    return r[u];
}

void reuneste(int u, int v)
{
    int r1 = reprezentare(u);
    int r2 = reprezentare(v);
    for(int k = 1; k <= n; k++)
        if(r[k] == r2)
            r[k] = r1;

}

int kruskal()
{
    sort(graf.begin(), graf.end(), cmp);
    for (int v = 1; v <= n; v++)
        initializare(v);
    int nr = 0;
    int cost = 0;
    for (auto x : graf)
        if(reprezentare(x.first.first) != reprezentare(x.first.second))
        {
            apcm.push_back(x);
            cost += x.second;
            reuneste(x.first.first, x.first.second);
            nr++;
            if(nr == n - 1)
                break;
        }
    return cost;
}

int main()
{
    f >> n >> m;
    for(int i = 0; i < m; i++)
    {
        int x, y, cost;
        f >> x >> y >> cost;
        graf.push_back(make_pair(make_pair(x, y), cost));
    }
    int cost_final = kruskal();
    g << cost_final << '\n' << n - 1 << '\n';
    for(auto x : apcm)
    {
        g << x.first.first << " " << x.first.second << '\n';
    }
    return 0;
}