Cod sursa(job #2532356)

Utilizator DragosSDragos Sarbu DragosS Data 27 ianuarie 2020 19:14:13
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include<bits/stdc++.h>
#define nmax 50003
#define oo 1e9
using namespace std;

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

vector < pair < int, int > > L[nmax];
priority_queue < pair < int, int > > Q;
int d[nmax];
int n, m;

int main()
{
    int i, x, y, c;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> c;
        L[x].push_back({y, c});
    }
    for(i = 2; i <= n; i++)
        d[i] = oo;
    d[1] = 0;
    Q.push({0, 1});
    while(!Q.empty())
    {
        x = Q.top().second;
        Q.pop();
        for(auto w : L[x])
        {
            if(d[x] + w.second < d[w.first])
            {
                d[w.first] = d[x] + w.second;
                Q.push({-d[w.first], w.first});
            }
        }
    }
    for(i = 2; i <= n; i++)
        if(d[i] == oo)
            fout << "0 ";
        else
            fout << d[i] << " ";
    fout << "\n";
}