Cod sursa(job #1785421)

Utilizator woogiefanBogdan Stanciu woogiefan Data 21 octombrie 2016 11:44:48
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

#define MAX 50010
#define inf 1e9

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
 
vector <pair<int, int> > G[MAX];
priority_queue <pair<int, int> > PQ;

int dist[MAX], viz[MAX];
int main()
{
    int n, m, x, y, c, i, nod;
    fin >> n >> m;
    while(m--)
    {
        fin >> x >> y >> c;
        G[x].push_back(make_pair(y, c));
    }
    dist[1] = 0;
    for(i = 2 ; i <= n ; i++)
    {
        dist[i] = inf;
    }
    PQ.push(make_pair(0, 1));
    while(PQ.size())
    {
        pair<int, int> aux = PQ.top();
        PQ.pop();
        nod = aux.second;
        if(viz[nod])
            continue;
        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(make_pair(-dist[it.first], it.first));
            }
    }
 
    for(i = 2 ; i <= n ; i++)
    {
        if(dist[i] == inf)
            dist[i] = 0;
        fout << dist[i] << " ";
    }
    fout << "\n";
 
 
}