Cod sursa(job #1778082)

Utilizator sebinechitasebi nechita sebinechita Data 13 octombrie 2016 13:46:07
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define MAX 50010
#define inf 1e9
#define cout fout
typedef vector <pair<int, int> > :: iterator iter;
vector <pair<int, int> > G[MAX];
int dist[MAX];
int nod, i, x, y, c, n, m;
priority_queue<pair<int, int> > Q;

int main()
{
    fin >> n >> m;
    while(m--)
    {
        fin >> x >> y >> c;
        G[x].push_back(make_pair(y, c));

    }
    for(i = 2 ; i <= n ; i++)
    {
        dist[i] = inf;
    }
    dist[1] = 0;
    Q.push(make_pair(0, 1));

    while(Q.size())
    {

        pair<int, int> aux = Q.top();
        Q.pop();
        nod = aux.second;
        if(-aux.first > dist[nod])
            continue;
       // cout << nod << " ";
        for(iter it = G[nod].begin() ; it != G[nod].end() ; it++)
        {
            if(dist[it->first] > dist[nod] + it->second)
            {
                dist[it->first] = dist[nod] + it->second;
                Q.push(make_pair(-dist[it->first], it->first));
            }
        }
    }
    for(i = 2 ; i <= n ; i++)
    {
        if(dist[i] == inf)
            dist[i] = 0;
        cout << dist[i] << ' ';
    }
    cout << "\n";
}