Cod sursa(job #2674311)

Utilizator Katherine456719Swan Katherine Katherine456719 Data 18 noiembrie 2020 22:07:12
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
using namespace std;

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

struct node{
    int value,cost;
};
int  costs[50005];
vector < pair<int, int> > graf[50005];
queue < node >heap;

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