Cod sursa(job #3310688)

Utilizator tonealexandruTone Alexandru tonealexandru Data 15 septembrie 2025 20:30:27
Problema Algoritmul lui Dijkstra Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
#define int long long

using namespace std;

priority_queue<pair<int, int>> pq;
vector<pair<int, int>> adj[250005];
int save[250005];

signed main()
{
    ifstream cin("dijkstra.in");
    ofstream cout("dijkstra.out");

    int n, m, a, b, val;
    cin>>n>>m;

    for(int i=0; i<=n; i++)
        save[i] = 1e17;

    for(int i=0; i<m; i++)
    {
        cin>>a>>b>>val;
        adj[a].push_back({b, val});
    }

    save[1] = 0;
    pq.push({0, 1}); /// <cost pana acolo, nod>

    while(!pq.empty())
    {
        int curr_cost = pq.top().first, curr_nod = pq.top().second;
        pq.pop();

        for(auto x : adj[curr_nod])
        {
            int nou_nod = x.first, nou_cost = x.second;
            if(save[nou_nod] > curr_cost + nou_cost)
            {
                save[nou_nod] = curr_cost + nou_cost;
                pq.push({curr_cost + nou_cost, nou_nod});
            }
        }
    }

    for(int i=2; i<=n; i++)
    {
        if(save[i] == 1e17)
            cout<<0;
        else
            cout<<save[i];
        cout<<" ";
    }

    return 0;
}