Cod sursa(job #2505131)

Utilizator MocalinnoMoca Andrei Catalin Mocalinno Data 6 decembrie 2019 11:30:55
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>
#define DAU  ios::sync_with_stdio(false); fin.tie(0); fout.tie(0);
#define PLEC fin.close(); fout.close(); return 0;
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
using PII = pair<int, int>;
using VP  = vector<PII>;
using VVP = vector<VP>;
using VI  = vector<int>;
int n, p, x, y, c;
VVP g;
VI d;
int main()
{
    DAU
    fin >> n >> p;
    g = VVP(n + 1);
    while (fin >> x >> y >> c)
        g[x].emplace_back(y, c);
    priority_queue<PII, VP, greater<PII>> q;
    d = VI(n + 1, 1e9);
    d[p] = 0;
    q.emplace(0, p);
    while (!q.empty())
    {
        x = q.top().second;
        q.pop();
        for (const PII& P : g[x])
        {
            y = P.first;
            c = P.second;
            if (d[y] > d[x] + c)
                d[y] = d[x] + c, q.emplace(d[y], y);
        }
    }
    for (int i = 1; i <= n; ++i)
        if (d[i] == 1e9)
            fout << "-1 ";
        else fout << d[i] << ' ';
    PLEC
}