Cod sursa(job #2172887)

Utilizator razvan99hHorhat Razvan razvan99h Data 15 martie 2018 18:43:04
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define DM 50005
#define INF 0x3f3f3f3f
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

int n, m, dist[DM], viz[DM], a, b ,c;
priority_queue < pair<int, int> > pq;
vector < pair<int, int> > g[DM];

void dijkstra()
{
    for(int i = 2; i <= n; i++)
        dist[i] = INF;
    pq.push({0, 1});

    while(!pq.empty())
    {
        int nod = pq.top().second;
        pq.pop();
        if(!viz[nod])
        {
            viz[nod] = 1;
            for(auto it : g[nod])
                if(dist[nod] + it.second < dist[it.first])
                {
                    dist[it.first] = dist[nod] + it.second ;
                    pq.push({-dist[it.first], it.first});
                }
        }
    }
}
int main ()
{
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> a >> b >> c;
        g[a].push_back({b, c});
    }

    dijkstra();

    for(int i = 2; i <= n; i++)
        fout << dist[i] << ' ';
    return 0;
}