Cod sursa(job #2696149)

Utilizator gasparrobert95Gaspar Robert Andrei gasparrobert95 Data 15 ianuarie 2021 14:39:55
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>
#define ll long long
#define LMAX 50005
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, dist[LMAX];
bool inpq[LMAX];
vector < pair <int, int> > g[LMAX];
priority_queue <int> pq;

void dijkstra() {
    for (int i = 2; i <= n; ++i)
        dist[i] = (1 << 30);
    pq.push(-1);
    inpq[1] = true;
    while (!pq.empty()) {
        int nod = abs(pq.top());
        pq.pop();
        for (int i = 0; i < g[nod].size(); ++i) {
            int next = g[nod][i].first, val = g[nod][i].second;
            if (dist[next] > dist[nod] + val) {
                dist[next] = dist[nod] + val;
                if (!inpq[next]) {
                    inpq[next] = true;
                    pq.push(-next);
                }
            }
        }
    }
    return;
}

int main() {
    fin >> n >> m;
    while (m--) {
        int a, b, c;
        fin >> a >> b >> c;
        g[a].push_back({b, c});
    }
    dijkstra();
    for (int i = 2; i <= n; ++i) {
        if (dist[i] < (1 << 30))
            fout << dist[i] << " ";
        else
            fout << 0 << " ";
    }
    return 0;
}