Cod sursa(job #1644016)

Utilizator george_stelianChichirim George george_stelian Data 9 martie 2016 21:12:00
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

struct edge
{
    int nod,c;
};
vector<edge> v[200010];
struct heap
{
    int nod,c,tata;
    bool operator <(const heap &aux) const
    {
        return c>aux.c;
    }
};
priority_queue<heap> h;
pair<int,int> sol[200010];
char vaz[200010];

void update(int nod)
{
    for(vector<edge>::iterator it=v[nod].begin();it!=v[nod].end();it++)
        if(!vaz[it->nod]) h.push({it->nod,it->c,nod});
}

int main()
{
    freopen("apm.in", "r", stdin);
    freopen("apm.out", "w", stdout);
    int n,m,x,y,c,nr=0,sum=0;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&c);
        v[x].push_back({y,c});
        v[y].push_back({x,c});
    }
    update(1);
    vaz[1]=1;
    while(!h.empty())
    {
        int nod=h.top().nod,c=h.top().c,tata=h.top().tata;
        h.pop();
        if(vaz[nod]) continue;
        vaz[nod]=1;
        sum+=c;
        sol[++nr]={nod,tata};
        update(nod);
    }
    printf("%d\n%d\n",sum,nr);
    for(int i=1;i<=nr;i++) printf("%d %d\n",sol[i].first,sol[i].second);
    return 0;
}