Cod sursa(job #2572880)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 5 martie 2020 14:45:16
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;

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

void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

const int N = 5e4 + 5;

vector < pair < int, int > > a[N];
vector < int > d(N, 2e9);


void dijkstra(int n)
{
    d[1] = 0;
    priority_queue < pair < int, int >, vector < pair < int, int > >, greater < pair < int , int > > > pq;
    pq.push({1, d[1]});
    while(!pq.empty()) {
        int v = pq.top().first;
        int len = pq.top().second;
        pq.pop();
        if(d[v] < len) continue;
        for(auto x : a[v]) {
            int to = x.first;
            int cost = x.second;
            if(d[to] > d[v] + cost) {
                d[to] = d[v] + cost;
                pq.push({to, d[to]});
            }
        }
    }
    for(int i = 2; i <= n; ++i) fout << d[i] << " ";
}

int main()
{
    usain_bolt();

    int n, m;

    fin >> n >> m;
    for(int i = 1; i <= m; ++i) {
        int x, y, w;

        fin >> x >> y >> w;
        a[x].push_back({y, w});
    }
    dijkstra(n);
    return 0;
}