Cod sursa(job #2435007)

Utilizator AlexNeaguAlexandru AlexNeagu Data 2 iulie 2019 19:14:44
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <vector>
#include <queue>
#define nmax 50005
#define mmax 250005
#define oo 2000000005
using namespace std;
priority_queue < pair < int, int > > Q;
vector < pair < int, int > > E[nmax + 5];
vector < int > D(nmax + 5, oo), F(nmax + 5, 0);
int n, m, cost, x, y;
ifstream cin ("dijkstra.in");
ofstream cout("dijkstra.out");
void Dijk()
{
    while(!Q.empty())
    {
        int node = Q.top().second;
        Q.pop();
        if (F[node]) continue;
        F[node] = 1;
        for (auto it : E[node])
            if (D[it.first] > D[node] + it.second)
        {
            D[it.first] = D[node] + it.second;
            Q.push({-D[it.first], it.first});
        }
    }
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
         cin >> x >> y >> cost;
         E[x].push_back({y, cost});
    }
    Q.push({0, 1});
    D[1] = 0;
    Dijk();
    for (int i = 2; i <= n; i++) D[i] == oo ? cout << "0 " : cout << D[i] << " ";
    return 0;
}