Cod sursa(job #3335298)

Utilizator anghelmrsmanghel eduard anghelmrsm Data 22 ianuarie 2026 11:21:30
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#include <climits>
#include <set>

using namespace std;

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

typedef pair <int, int> edge;

vector <edge> vecini[50001];
int n, m, dist[50001];
priority_queue <edge, vector <edge>, greater <edge>> pq;

void Djikstra(int start)
{
    for (int i=1; i<=n; i++)
        dist[i] = INT_MAX;

    dist[start] = 0;
    pq.push({0, start});

    while (!pq.empty())
    {
        int d = pq.top().first;
        int i = pq.top().second;
        pq.pop();

        if (d > dist[i])
            continue;

        for (auto edge : vecini[i])
        {
            int j = edge.second;
            int w = edge.first;

            if (dist[i] + w < dist[j])
            {
                dist[j] = dist[i] + w;
                pq.push({dist[j], j});
            }
        }
    }
}

int main()
{
    cin>>n>>m;
    for (int i=0; i<m; i++)
    {
        int x, y, cost;
        cin>>x>>y>>cost;
        vecini[x].push_back({cost, y});
    }

    Djikstra(1);

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