Cod sursa(job #2309464)

Utilizator AndreiLunguLungu Andrei Sebastian AndreiLungu Data 29 decembrie 2018 00:36:30
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <bits/stdc++.h>
#define N 300005
#define oo 1000000000
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n , m , viz[N] , d[N];
        ///   nod   cost
vector < pair<int , int> > L[N];
        ///           cost   nod
priority_queue < pair <int , int> > q;
void Citire()
{
    int i , x , y , c;
    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> c;
        L[x].push_back({y , c});
    }
    fin.close();
}
void Dijkstra(int start)
{
    int i , k , cost;
    for(i = 1; i <= n; i++)
        d[i] = oo;
    q.push({0 , start});
    d[start] = 0;
    viz[start] = 1;
    while(!q.empty())
    {
        k = q.top().second;
        q.pop();
        viz[k] = 0;
        for(auto j : L[k])
        {
            i = j.first;
            cost = j.second;
            if(d[i] > d[k] + cost)
            {
                d[i] = d[k] + cost;
                if(viz[i] == 0)
                {
                    viz[i] = 1;
                    q.push({d[i] , i});
                }
            }
        }
    }
    for(i = 2; i <= n; i++)
        fout << d[i] << " ";
    fout.close();
}
int main()
{
    Citire();
    Dijkstra(1);
    return 0;
}