Cod sursa(job #1885801)

Utilizator alingeorgiu99@yahoo.comGeorgiu Alin Ionel [email protected] Data 20 februarie 2017 13:23:39
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 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(fin>>x>>y>>c)
    {
        G[x].push_back(make_pair(c, y));
    }
    for(i = 1 ; i <= n ; i++)
    {
        dist[i] = inf;
    }
    dist[1] = 0;
    PQ.push(make_pair(0, 1));

    while(PQ.size())
    {
        PQ.top();
    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.first<dist[it.second])
            {
                dist[it.second]=dist[nod]+it.first;
                PQ.push(make_pair(-dist[it.second],it.second));
            }
    }
    for(i = 2 ; i <= n ; i++)
    {
        if(dist[i] == inf)
            dist[i] = -1;
        cout << dist[i] << " ";
    }
    fin.close();
    fout.close();
    return 0;
}