Cod sursa(job #1510388)

Utilizator sebinechitasebi nechita sebinechita Data 24 octombrie 2015 21:58:00
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define MAX 50010
#define INF (1<<19)
typedef vector<pair <int, int> > :: iterator iter;
vector <pair <int, int> > G[MAX];

priority_queue <pair <int, int> > Q;
int viz[MAX], dist[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));
    }
    for(i = 2 ; i <= n ; i++)
        dist[i] = INF;
    Q.push(make_pair(0, 1));
    while(Q.size())
    {
        nod = Q.top().second;
        Q.pop();
        if(viz[nod])
            continue;
        viz[nod] = 1;
        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++)
    {
        fout << ((dist[i] == INF) ? 0 : dist[i]) << " ";
    }
    fout << "\n";
}