Cod sursa(job #2350657)

Utilizator EclipseTepes Alexandru Eclipse Data 21 februarie 2019 17:07:53
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <fstream>
#define dmax 50005
#define INF 99999999

using namespace std;

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

int dist[dmax];
int pVerif, newV;

struct Compare{
    bool operator() (int e1, int e2) {
        return dist[e1] > dist[e2];
    }
};

int n, m, x, y, c;
vector<pair<int, int> > graf[dmax];
priority_queue<int, vector<int>, Compare> pq;
bool viz[dmax];

int main() {
    int i, j;
    cin >> n >> m;
    for (i = 1; i <= m; i++) {
        cin >> x >> y >> c;
        graf[x].push_back({y, c});
        graf[y].push_back({x, c});
    }
    for (i = 1; i <= n; i++) dist[i] = INF;
    dist[1] = 0;
    viz[1] = true;
    pq.push(1);
    while (!pq.empty()) {
        pVerif = pq.top();
        pq.pop();
        viz[pVerif] = false;
        for (i = 0; i < graf[pVerif].size(); i++) {
            newV = graf[pVerif][i].first;
            if (dist[newV] > dist[pVerif] + graf[pVerif][i].second) {
                dist[newV] = dist[pVerif] + graf[pVerif][i].second;
                if (!viz[newV]) {
                    pq.push(newV);
                    viz[newV] = true;
                }
            }
        }
    }
    for (i = 2; i <= n; i++) {
        if (dist[i] == INF) cout << "0 ";
        else cout << dist[i] << ' ';
    }

    return 0;
}