Cod sursa(job #3235637)

Utilizator Sasha_12454Eric Paturan Sasha_12454 Data 19 iunie 2024 14:12:12
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>

std :: ifstream in ("dijkstra.in");

std :: ofstream out ("dijkstra.out");

const int NMAX = 5e4 + 5;

const int INF = (1 << 30);

int n;

int m;

int x;

int y;

int d;

std :: vector <int> dist(NMAX, INF);

std :: vector <std :: pair<int, int>> v[NMAX];

std :: priority_queue <std :: pair<int, int>> pq;

void dijkstra()
{
    pq.push(std :: make_pair(0, 1));

    while(!pq.empty())
    {
        int nod = pq.top().second;

        pq.pop();

        for(auto i : v[nod])
        {
            if(dist[nod] + i.second < dist[i.first])
            {
                dist[i.first] = dist[nod] + i.second;

                pq.push(std :: make_pair(-dist[i.first], i.first));
            }
        }
    }
}

int main()
{

    in >> n >> m;

    for(int i = 1; i <= m; i ++)
    {
        in >> x >> y >> d;

        v[x].push_back(std :: make_pair(y, d));
    }

    dist[1] = 0;

    dijkstra();

    for(int i = 2; i <= n; i ++)
    {
        if(dist[i] == INF)
        {
            out << 0 << " ";
        }
        else
        {
            out << dist[i] << " ";
        }
    }

    return 0;
}