Cod sursa(job #3134090)

Utilizator ggaaggaabbiigoteciuc gabriel ggaaggaabbii Data 28 mai 2023 14:23:59
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;

#define MAXN 50010

ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector<pair<int, int>> G[MAXN];

priority_queue<pair<int, int>> PQ;

int n, m, x, y, c, dist[MAXN], viz[MAXN], nod;

int main()
{
    f >> n >> m;
    while (m--) {
        f >> x >> y >> c;
        G[x].push_back({y, c});
    }

    for (int i = 2; i <= n; ++i) {
        dist[i] = INT_MAX;
    }

    dist[1] = 0;

    PQ.push({0, 1});
    while (PQ.size()) {
        auto aux = PQ.top();
        PQ.pop();
        int nod = aux.second;
        if (!viz[nod]) {
            viz[nod] = 1;
            for (auto it:G[nod]) {
                if (dist[nod] + it.second < dist[it.first]) {
                    dist[it.first] = dist[nod] + it.second;
                    PQ.push({-dist[it.first], it.first});
                }
            }
        }
    }

    for (int i = 2; i <= n; ++i) {
        if (dist[i] == INT_MAX) {
            dist[i] = 0;
        }
        g << dist[i] << ' ';
    }

    return 0;
}