Cod sursa(job #3336588)

Utilizator anghelmrsmanghel eduard anghelmrsm Data 24 ianuarie 2026 22:47:42
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>

using namespace std;

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

struct edge{
    int i, j, w;
};

struct cmp{
    bool operator() (edge x, edge y)
    {
        return x.w > y.w;
    }
};

int n, m, total;
vector <edge> vecini[200001], mst;
priority_queue <edge, vector <edge>, cmp> pq;
bitset <200001> viz;
int main()
{
    f>>n>>m;
    for (int i=1;i<=m;i++)
    {
        int x, y, w;
        f>>x>>y>>w;
        vecini[x].push_back({x, y, w});
        vecini[y].push_back({y, x, w});
    }

    viz[1] = 1;
    for (auto e : vecini[1])
        pq.push(e);

    while (!pq.empty() and mst.size() < n)
    {
        edge e = pq.top();
        pq.pop();

        if (viz[e.j] == 0)
        {
            int i = e.j;
            viz[i] = 1;
            total += e.w;
            mst.push_back(e);

            for (auto e : vecini[i])
                pq.push(e);
        }

    }
    g<<total;
}