Cod sursa(job #3155855)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 9 octombrie 2023 21:49:28
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int max_size = 2e5 + 1, INF = 2e9 + 1;

struct str{
    int nod, cost;
    bool operator < (const str & aux) const
    {
        return cost > aux.cost;
    }
};

int d[max_size], t[max_size], uz[max_size], ans;
vector <pair <int, int>> mc[max_size], apm;
priority_queue <str> pq;

void djk ()
{
    pq.push({1, 0});
    while (!pq.empty())
    {
        int nod = pq.top().nod, val = pq.top().cost;
        pq.pop();
        if (uz[nod])
        {
            continue;
        }
        uz[nod] = 1;
        if (nod != 1)
        {
            ans += val;
            apm.push_back({t[nod], nod});
        }
        for (auto f : mc[nod])
        {
            if (f.second < d[f.first] && !uz[f.first])
            {
                t[f.first] = nod;
                d[f.first] = f.second;
                pq.push({f.first, d[f.first]});
            }
        }
    }
}

int main ()
{
    int n, m;
    in >> n >> m;
    while (m--)
    {
        int x, y, c;
        in >> x >> y >> c;
        mc[x].push_back({y, c});
        mc[y].push_back({x, c});
    }
    for (int i = 2; i <= n; i++)
    {
        d[i] = INF;
    }
    djk();
    out << ans << '\n' << n - 1 << '\n';
    for (auto f : apm)
    {
        out << f.first << " " << f.second << '\n';
    }
}