Cod sursa(job #2706408)

Utilizator NeganAlex Mihalcea Negan Data 14 februarie 2021 19:58:40
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <bits/stdc++.h>

using namespace std;

const int oo = 2000000000;

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

vector < pair < int, int > > V[50100];
priority_queue < pair < int, int> > pq;
int n, m, k, dist[50100];
bitset <50100> viz;

void Citire()
{
    int i, x, y, val;
    fin >> n >> m;
    for(i = 1;i <= m;i++)
    {
        fin >> x >> y >> val;
        V[x].push_back({y, val});
    }
}

void Dijkstra()
{
    int i, j, x, y;
    for(i = 1;i <= n;i++)
        dist[i] = oo;
    dist[1] = 0;
    pq.push({0, 1});
    while(!pq.empty())
    {
        int nod = pq.top().second;
        pq.pop();
        if(viz[nod] == 0)
        {
            viz[nod] = 1;
            for(auto x : V[nod])
                if(dist[x.first] > dist[nod] + x.second)
                {
                    dist[x.first] = dist[nod] + x.second;
                    pq.push({-dist[x.first], x.first});
                }
        }
    }
}
void Rezolva()
{
    for(int i = 2;i <= n;i++)
        if(dist[i] == oo)
            fout << "0 ";
        else
            fout << dist[i] << " ";
}

int main()
{
    int i, x, y, sum = 0;
    Citire();
    Dijkstra();
    Rezolva();
    return 0;
}