Cod sursa(job #2017737)

Utilizator MaligMamaliga cu smantana Malig Data 2 septembrie 2017 12:06:06
Problema Arbore partial de cost minim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.6 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
#include <deque>
#include <ctime>
#include <queue>

#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
using zint = int;
using namespace std;
const int inf = 1e9 + 5;
const int NMax = 2e5 + 5;
ifstream in("apm.in");
ofstream out("apm.out");

zint N,M;
bool inApm[NMax];
zint lung[NMax];

struct elem {
    zint x,y,c;
    elem(zint _x = 0,zint _y = 0,zint _c = 0) {
        x = _x;
        y = _y;
        c = _c;
    }

};
vector<elem> v[NMax], edge;

bool operator< (elem a,elem b) {
    return a.c > b.c;
}

int main() {
    //*
    in>>N>>M;
    for (zint i=1;i <= M;++i) {
        zint x,y,c;
        in>>x>>y>>c;

        v[x].pb(elem(x,y,c));
        v[y].pb(elem(y,x,c));
    }

    for (zint i=1;i <= N;++i) {
        lung[i] = inf;
    }
    lung[1] = 0;

    priority_queue< elem > Q;
    Q.push(elem(0,1,0));
    zint costArb = 0;
    while (Q.size()) {
        auto p = Q.top();
        Q.pop();
        if (inApm[p.y]) {
            continue;
        }

        costArb += p.c;
        inApm[p.y] = true;
        edge.pb(p);
        for (auto e : v[p.y]) {
            int nxt = e.y, cost = e.c;

            if (lung[nxt] > cost) {
                lung[nxt] = cost;
                Q.push(elem(e.x,nxt,cost));
            }
        }
    }
    out<<costArb<<'\n'<<N-1<<'\n';
    for (zint i=1;i < edge.size();++i) {
        out<<edge[i].x<<' '<<edge[i].y<<'\n';
    }
    //*/

    in.close();out.close();
    return 0;
}