Cod sursa(job #2387995)

Utilizator alisavaAlin Sava alisava Data 25 martie 2019 16:09:42
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
priority_queue<pair <int,int> >q;   /// cost, nod
vector< pair <int, int> > v[50005];   /// muchie, cost
int c[50005];
bool viz[50005];
int n, m;
void Citire()
{
    int x, y, cost;
    fin >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        fin >> x >> y >> cost;
        v[x].push_back(make_pair(y, cost));
    }
    for(int i = 1; i <= n; i++)
        c[i] = 1e9;
}
void Dijkstra()
{
    int x;
    q.push(make_pair(0, 1));
    c[1] = 0;
    while(!q.empty())
    {
        x = q.top().second;
        q.pop();
        if(!viz[x])
        {
            viz[x] = true;
            for(int i = 0; i < v[x].size(); i++)
            {
                if(c[x] + v[x][i].second < c[v[x][i].first])
                {
                    c[v[x][i].first] = c[x] + v[x][i].second;
                    q.push(make_pair(c[v[x][i].first], v[x][i].first));
                }
            }
        }
    }
}
void Afisare()
{
    for(int i = 2; i <= n; i++)
        fout << c[i] << " ";



}

int main()
{
    Citire();
    Dijkstra();
    Afisare();

    return 0;
}