Cod sursa(job #2824902)

Utilizator apocal1ps13Stefan Oprea Antoniu apocal1ps13 Data 3 ianuarie 2022 18:14:51
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>


using namespace std;
#define Inf 0x3f3f3f3f

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

using PI = pair<int, int>;
int n, m, x, y, p, D[100001], w;
vector <PI> G[100001];

void dijkstra(int nod)
{
    priority_queue < PI, vector<PI>, greater<PI> > Q;
    D[nod] = 0;
    Q.push({ 0 , nod });
    while (!Q.empty())
    {
        x = Q.top().first;
        y = Q.top().second;
        Q.pop();
        if (x > D[y]) continue;
        for (auto& q : G[y])
        {
            int nodnou = q.first;
            int costnou = q.second;
            if (D[nodnou] > D[y] + costnou)
            {
                D[nodnou] = D[y] + costnou;
                Q.push({ D[nodnou] , nodnou });
            }
        }
    }
}

int main()
{
    in >> n >> m >> p;
    for (int i = 1; i <= m; ++i)
    {
        in >> x >> y;
        G[x].push_back({ y , w });
    }
    for (int i = 1; i <= n; i++)
        D[i] = Inf;
    dijkstra(1);
    for (int i = 2; i <= n; i++)
        if (D[i] == Inf) out << "0 ";
        else out << D[i] << " ";
}