Cod sursa(job #2362853)

Utilizator lupulescu2001Lupulescu Vlad lupulescu2001 Data 3 martie 2019 12:05:04
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>
#include <queue>
#include <vector>
#include <functional>

#define deb 0
#define deb2 1

using namespace std;

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

const int oo = 2000000000;
const int nMax = 50000;
vector<pair<int, int>> g[nMax + 5];
int n, m;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> heap;
int dp[nMax + 5], use[nMax + 5];

void Read() {
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y, c;
        fin >> x >> y >> c;
        g[x].push_back({y, c});
    }
}

void Dijkstra() {
    for (int i = 2; i <= n; i++)
        dp[i] = oo;
    heap.push({0, 1});
    use[1] = 1;
    while (!heap.empty()) {
        int nod = heap.top().second;
        int cost = heap.top().first;
        heap.pop();
        if (dp[nod] != cost)
            continue;
        for (auto i : g[nod]) {
            if (dp[i.first] > cost + i.second) {
                dp[i.first] = cost + i.second;
                heap.push({dp[i.first], i.first});
            }
        }
    }
}

int main() {
    Read();
    Dijkstra();
    for (int i = 2; i <= n; i++)
        if (dp[i] == oo)
            fout << 0 << " ";
        else
            fout << dp[i] << " ";
    fout << '\n';
    return 0;
}