Cod sursa(job #2675833)

Utilizator Katherine456719Swan Katherine Katherine456719 Data 22 noiembrie 2020 17:24:19
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>
using namespace std;

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

struct node{
    int value,cost;
    bool operator <(const node &other) const{
        return cost > other.cost;
    }
};

int  costs[50005];
vector < pair<int, int> > graf[50005];
priority_queue < node > heap;

int main() {
    int n, m;
    fin >> n >> m;
    for(int i = 1;i <=n; ++i )
        costs[i] = 999999999;
    for(int i = 1; i <= m; ++i)
    {
        int x, y, costsn;
        fin >> x >> y >> costsn;
        graf[x].push_back({y,costsn});
    }
    costs[1] = 0;
    heap.push({1,0});
    while(!heap.empty())
    {
        node curent;
        curent.value = heap.top().value;
        curent.cost = heap.top().cost;
        heap.pop();
        if(curent.cost != costs[curent.value])continue;
        for(auto x : graf[curent.value])
        {
            if(costs[x.first] > costs[curent.value] + x.second) {
                costs[x.first] = min(costs[x.first], costs[curent.value] + x.second);
                heap.push({x.first, costs[x.first]});
            }
        }
    }
    for(int i = 2;i <= n; ++i)
        if(costs[i] == 999999999)
            fout << 0 << " ";
        else
            fout << costs[i] << " ";
    return 0;
}