Cod sursa(job #3287313)

Utilizator BucsMateMate Bucs BucsMate Data 17 martie 2025 15:58:44
Problema Arbore partial de cost minim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.16 kb
/*#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

const int INF = 1 << 29;

ifstream input("prim.in");
ofstream output("prim.out");

int main()
{
    int n, m;
    input >> n >> m;

    vector<vector<pair<int, int>>> adj(n+1);

    for(int i = 0; i < m; i++){
        int a, b, c;
        input >> a >> b >> c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, c});
    }

    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    vector<int> key(n+1, INF);
    vector<int> parent(n+1);
    vector<bool> inMST(n+1, false);

    key[1] = 0;
    parent[1] = 0;
    pq.push({key[1], 1});

    while(!pq.empty()){
        pair<int, int> curr = pq.top();
        pq.pop();
        int currNode = curr.second;
        int currKey = curr.first;
        inMST[currNode] = true;

        for(int i = 0; i < adj[currNode].size(); i++){
            if(inMST[adj[currNode][i].first] == false && key[adj[currNode][i].first] > adj[currNode][i].second){
                key[adj[currNode][i].first] = adj[currNode][i].second;
                pq.push({key[adj[currNode][i].first], adj[currNode][i].first});
                parent[adj[currNode][i].first] = currNode;
            }
        }
    }
    int cost = 0;
    for(int i = 1; i <= n; i++){
        cost += key[i];
    }
    output << cost << endl;
    for(int i = 1; i <= n; i++){
        output << parent[i] << " ";
    }
    return 0;
}
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

const int INF = 1 << 29;

ifstream fin("prim.in");
ofstream fout("prim.out");

int prim(vector<vector<pair<int, int>>> &adj, int n, vector<pair<int, int>> &mst_edges)
{
    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
    vector<bool> visited(n+1, false);
    int res = 0;
    pq.push({0, {-1, 1}});
    //visited[1] = true;
    while(!pq.empty()){
        int currNode = pq.top().second.second;
        int lastNode = pq.top().second.first;
        int cost = pq.top().first;
        pq.pop();
        if(visited[currNode]){
            continue;
        }
        visited[currNode] = true;
        mst_edges.push_back({lastNode, currNode});
        res += cost;
        for(int i = 0; i < adj[currNode].size(); i++){
            int newNode = adj[currNode][i].first;
            int add_cost = adj[currNode][i].second;
            if(!visited[newNode]){
                pq.push({add_cost, {currNode, newNode}});
            }
        }
    }
    return res;
}

int main()
{
    int n, m;
    fin >> n >> m;

    vector<vector<pair<int, int>>> adj(n+1);

    for(int i = 0; i < m; i++){
        int a, b, c;
        fin >> a >> b >> c;
        adj[a].push_back({b, c});
        adj[b].push_back({a, c});
    }
    vector<pair<int, int>> mst_edges;
    fout << prim(adj, n, mst_edges) << endl;
    fout << n-1 << endl;
    for(int i = 1; i < mst_edges.size(); i++){
        fout << mst_edges[i].first << " " << mst_edges[i].second << endl;
    }
    return 0;
}