Cod sursa(job #3286958)

Utilizator AswVwsACamburu Luca AswVwsA Data 14 martie 2025 20:54:15
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;

const int NMAX = 2e5;

vector <pair <int, int> > g[NMAX + 1];

struct ob
{
    int muchie;
    int n1, n2;
};

bool operator <(ob a, ob b)
{
    return a.muchie > b.muchie;
}

priority_queue <ob> pq;

bool viz[NMAX + 1];

vector <pair <int, int> > sol;

int main()
{
    ifstream cin("apm.in");
    ofstream cout("apm.out");
    int n, m, i;
    cin >> n >> m;
    for (i = 1; i <= m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
    int ans = 0;
    int last = 1;
    viz[1] = 1;
    for (i = 1; i <= n; i++)
    {
        for (pair <int, int> x : g[last])
        {
            pq.push({x.second, last, x.first});
        }
        bool done = 0;
        while (!pq.empty() and !done)
        {
            ob f = pq.top();
            pq.pop();
            if (!viz[f.n2])
            {
                viz[f.n2] = 1;
                sol.push_back({f.n1, f.n2});
                last = f.n2;
                ans += f.muchie;
                done = 1;
            }
        }
    }
    cout << ans << "\n";
    cout << sol.size() << "\n";
    for (pair <int, int> x : sol)
        cout << x.first << " " << x.second << "\n";
}