Cod sursa(job #3310168)

Utilizator MerlinTheWizardMelvin Abibula MerlinTheWizard Data 12 septembrie 2025 00:05:38
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 5e4 + 5, INF = 1e9 + 7;
int n, m, dist[NMAX];
vector <pair<int, int>> v[NMAX];
bool viz[NMAX];
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    freopen("dijkstra.in", "r", stdin);
    freopen("dijkstra.out", "w", stdout);

    cin >> n >> m;
    int a, b, c;
    for(int i = 1; i <= m; i++)
    {
        cin >> a >> b >> c;
        v[a].push_back({b, c});
    }

    q.push({0, 1});
    while (!q.empty()) 
    {
        int cost = q.top().first, node = q.top().second;
        q.pop();

        if(!viz[node])
        {
            viz[node] = 1;
            dist[node] = cost;
            for (auto u : v[node]) 
            {
                if (!viz[u.first]) 
                    q.push({u.second + cost, u.first});
            }
        }
    }


    for(int i = 2; i <= n; i++)
    {
        cout << dist[i] << " ";
    }
}