Cod sursa(job #2752332)

Utilizator mihai_700Mihai Barbulescu mihai_700 Data 17 mai 2021 18:19:59
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream in("apm.in");
ofstream out("apm.out");

const int N = 200001;
const INF = 1e9;

int n, m, d[N], pred[N], cost;

vector <pair<int, int>> a[N];
bitset <N> selectat;
priority_queue <pair<int, int>> pq;

void apm()
{
    pq.push({0, 1});
    for (int i = 2; i <= n; ++i)
        d[i] = INF;

    int x;
    while (!pq.empty())
    {
        x = pq.top().second;
        pq.pop();
        if (selectat[x])
            continue;
        
        selectat[x] = true;
        cost += d[x];

        for (auto p: a[x])
        {
            int y = p.first;
            int c = p.second;
            if (!selectat[y] && c < d[y])
            {
                pred[y] = x;
                d[y] = c;
                pq.push({-c, y});
            }
        }
    }
}

int main()
{
    in >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        int x, y, c;
        in >> x >> y >> c;
        a[x].push_back({y, c});
        a[y].push_back({x, c});
    }

    out << cost << '\n' << n - 1 << '\n';
    for (int i = 2; i <= n; ++i)
    {
        out << i << ' ' << pred[i] << '\n';
    }

    return 0;
}