Cod sursa(job #2530704)

Utilizator ikogamesIon Ceaun ikogames Data 25 ianuarie 2020 09:56:33
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
#define nmax 50003
#define oo 1e9
using namespace std;

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

///                  cost nod
priority_queue< pair<int, int> > q;
int d[nmax];
          ///node  cost
vector < pair<int, int> > L[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])
        {
            i = w.first;
            if (d[i] > d[x] + w.second)
            {
                d[i] = d[x] + w.second;
                q.push({-d[i], i});
            }
        }
    }
    for(i = 2; i <= n; i++)
        if(d[i] == oo) fout << "0 ";
           else  fout << d[i] << " ";
    return 0;
}