Cod sursa(job #3335907)

Utilizator and_Turcu Andrei and_ Data 23 ianuarie 2026 20:37:24
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 50007;

vector<pair<int, int>> a[N];
vector<int> dist(N, -1);
int n, m;

void calc_dist()
{
    priority_queue< pair<int, int> > pq;
    pq.push({0, 1});

    while(!pq.empty())
    {
        int x = pq.top().second;
        int c = -pq.top().first;
        pq.pop();
cerr << x << "\n";
        if(dist[x] == -1)
        {
            dist[x] = c;

            for(auto pw : a[x])
            {
                int y = pw.second;
                int d = pw.first;

                if(dist[y] == -1)
                    pq.push({-(c + d), y});

            }

        }
    }

}

int main()
{
    fin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y, c;
        fin >> x >> y >> c;
        a[x].push_back({c, y});
    }

    calc_dist();

    for(int i = 2; i <= n; i++)
        fout << dist[i] << " ";

    return 0;
}