Cod sursa(job #2082091)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 5 decembrie 2017 18:27:37
Problema Arbore partial de cost minim Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#include <vector>
#include <queue>


using namespace std;

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

const int nmax = 400005;
struct edge {
    int x, y, c;
};

struct cmp {
    bool operator()(edge a, edge b) {
        return a.c > b.c;
    }
};

edge sol[2*nmax];
priority_queue<edge, vector<edge>, cmp> top;
vector<edge> ls[nmax];
int n, m, x, y, c, i, yy, viz[nmax], cst[nmax], N;
long long sm;

int main() {
    f >> n >> m;
    for (i = 1; i <= m; i++) {
        f >> x >> y >> c;
        ls[x].push_back({x,y,c});
        ls[y].push_back({y,x,c});
    }
    for (i = 1; i <= n; i++)
        cst[i] = 10000;

    viz[1] = 1;
    top.push({0,1,0});
    while (top.empty() == 0) {
        edge v = top.top();
        top.pop();
        x = v.x;
        y = v.y;
        c = v.c;
        if (y != 1 && viz[y] == 0) {
            viz[y] = 1;
            sol[++N] = {x,y,100};
            sm += c;
        }

        for (vector<edge>::iterator z = ls[y].begin(); z != ls[y].end(); z++) {
            yy = z->y;
            c = z->c;
            if (viz[yy] == 0 && cst[yy] > c) {
                cst[yy] = c;
                top.push({y,yy,c});
            }
        }
    }
    g << sm << '\n' << N << '\n';
    for (i = 1; i <= N; i++)
        g << sol[i].x << ' ' << sol[i].y << '\n';
}