Cod sursa(job #2722529)

Utilizator NeganAlex Mihalcea Negan Data 12 martie 2021 22:45:52
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

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

const int oo = 2000000000;
int n, m;
int dist[50100], viz[50100];
vector <pair <int, int> > v[50100];
void Dijkstra(int nod)
{
    int i, j, x, y;
    for(i = 1;i <= n;i++)
            dist[i] = oo;
    dist[nod] = 0;
    priority_queue< pair <int, int> > pq;
    pq.push({0, 1});
    while(!pq.empty())
    {
        int k = pq.top().second;
        pq.pop();
        for(auto w : v[k])
            if(viz[w.first] == 0)
            {
                viz[w.first] = 1;
                if(dist[w.first] > dist[k] + w.second)
                {
                    dist[w.first] = dist[k] + w.second;
                    pq.push({-dist[w.first], w.first});
                }
            }
    }
    for(i = 2;i <= n;i++)
        fout << dist[i] << " ";
}
int main()
{
    int i;
    fin >> n >> m;
    for(i = 1;i <= m;i++)
    {
        int x, y, cost;
        fin >> x >> y >> cost;
        v[x].push_back({y, cost});
    }
    Dijkstra(1);

    return 0;
}