Cod sursa(job #2537972)

Utilizator CiboAndreiAndrei Cibo CiboAndrei Data 4 februarie 2020 10:37:46
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;

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

const int dim = 50001;
const int oo = 200000001;

int n, m;
vector < pair <int, int> > v[dim];
int dist[dim];

void dijkstra(int nods){
    priority_queue < pair <int, int> > pq;

    pq.push({0, nods});

    while(!pq.empty()){
        auto frnt = pq.top(); pq.pop();

        for(auto it: v[frnt.second]){
            if(dist[frnt.second] + it.second < dist[it.first]){
                dist[it.first] = dist[frnt.second] + it.second;
                pq.push({-dist[it.first], it.first});
            }
        }
    }
}

int main()
{
    int i, j, a, b, c;

    f >> n >> m;

    for(i = 1; i <= m; ++i){
        f >> a >> b >> c;
        v[a].push_back({b, c});
    }

    fill_n(dist + 2, n + 1, oo);

    dijkstra(1);

    for(i = 2; i <= n; ++i)
        g << (dist[i] < oo ? dist[i] : 0) << " ";

    return 0;
}