Cod sursa(job #3142439)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 21 iulie 2023 12:00:38
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.87 kb
#include <bits/stdc++.h>
#define P pair<int, int>
#define inf 1e7

using namespace std;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, i, x, y, c;
vector<P> l[50002];
priority_queue<P> q;
int d[50002];

static inline void calc() {
    int c, x;
    while(!q.empty()) {
        c = q.top().first;
        x = q.top().second;

        if(d[x] > -c) d[x] = -c;
        q.pop();
        if(d[x] >= -c) {
            for(auto it : l[x]) {
                if(d[it.first] == inf) q.push({c - it.second, it.first});
            }
        }
    }
}

int main() {
    fin >> n >> m;
    while(fin >> x >> y >> c) l[x].push_back({y, c});
    for(i = 1; i <= n; i++) d[i] = inf;
    q.push({0, 1});
    calc();
    for(i = 2; i <= n; i++) {
        if(d[i] == inf) fout << "0 ";
        else fout << d[i] << " ";
    }

    return 0;
}