Cod sursa(job #2459348)

Utilizator Anakin1001George Giorgiu Gica Anakin1001 Data 22 septembrie 2019 12:17:11
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#include <climits>
#include <queue>
#include <vector>
#define inf INT_MAX
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int viz[50001], i, x, y, n, m, d[50001], c, Min, node;
struct cmp{
    bool operator() (int &lhs, int &rhs) const{
        return d[lhs] > d[rhs];
    }
};
vector < pair <int, int> > graph[50001];
priority_queue <int, vector <int>, cmp > heap;
int main()
{   f >> n >> m;
    for(i = 1; i <= m; i++){
        f >> x >> y >> c;
        graph[x].push_back({y,c});
    }
    for(i = 2; i <= n; i++)
        d[i] = inf;
    heap.push(1);
    while(!heap.empty()){
        if(viz[heap.top()] == 1){
            heap.pop();
            continue;
        }
        node = heap.top();
        for(i = 0; i < graph[node].size(); i++){
            int new_node = graph[node][i].first;
            int cost = graph[node][i].second;
            if(d[new_node] > d[node] + cost && viz[new_node] == 0){
                d[new_node] = d[node] + cost;
                heap.push(new_node);
                viz[new_node] = 0;
            }
        }
        viz[node] = 1;
    }
    for(i = 2; i <= n; i++)
        if(d[i] == inf)
            g << 0 << ' ';
        else
            g << d[i] << ' ';
    return 0;
}