Cod sursa(job #2677536)

Utilizator ionicaion ionica Data 26 noiembrie 2020 19:27:27
Problema Arbore partial de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 kb
///Alg lui Prim
#include <bits/stdc++.h>
#define NM 200001
using namespace std;
///ifstream fin ("prim.in");
///ofstream fout ("prim.out");
ifstream fin ("apm.in");
ofstream fout ("apm.out");
void read();
int n, m, t[NM], ct,viz[NM];
struct pereche
{
    int y,c;
};

struct muchie
{
    int x, y, c;
};

struct cmp
{
    bool operator()(muchie u, muchie v)
    {
        return u.c > v.c; ///ca sa avem min-heap
    }
};


priority_queue<muchie, vector<muchie>, cmp> q;

vector<pereche> G[NM];

void citeste()
{
    int x,y,i,cost;
    fin >> n >> m;
    for(i=1; i<=m; i++)
    {
        pereche p;
        fin >> x >> y >> cost;
        p={y,cost};
        G[x].push_back(p);
        p={x,cost};
        G[y].push_back(p);
    }
}

int main()
{
    int i,nr;
    muchie e;
    pereche p;

    citeste();
    for(i=0;i<G[1].size();i++)
    {
       p=G[1][i];
       q.push({1, p.y, p.c});
       t[p.y]=1;
    }
    viz[1]=1;
    nr=0;
    while(nr<n-1)
    {
        e= q.top();
        q.pop();

        if(viz[e.y]==0)
        {
            nr++;
            viz[e.y]=1;
            t[e.y]=e.x;
            ct=ct+e.c;
            for(i=0;i<G[e.y].size();i++)
            {
             	p=G[e.y][i];
                if(viz[p.y]==0)
                    q.push({e.y, p.y,p.c});
            }
        }
    }
    fout << ct << '\n';
    /**
    fout<< 0 << ' ';
    for(i=2; i<=n; i++)
        fout << t[i] << ' ';
    **/
    fout<<n-1<<'\n';
    for(i=2;i<=n;i++)
        fout<<i<<' '<<t[i]<<'\n';
    return 0;
}