Cod sursa(job #3325337)

Utilizator temp1234Temp Mail temp1234 Data 25 noiembrie 2025 12:39:32
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 5e5 + 1;
const int INF = 1e9;

vector< pair<int, int> > adj[nmax];
int d[nmax], vis[nmax];
priority_queue < pair<int, int> > pq;

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


    for(int i = 1; i <= n; i ++) {
        d[i] = INF;
    }
    d[1] = 0;
    pq.push({-d[1], 1});
    while(!pq.empty()) {
        int nod = pq.top().second;
        pq.pop();
        if(vis[nod] == 1) {
            continue;
        }
        vis[nod] = 1;
        for(auto& v : adj[nod]) {
            int vecin = v.first;
            int cost = v.second;
            if(d[vecin] > d[nod] + cost) {
                d[vecin] = d[nod] + cost;
                pq.push({-d[vecin], vecin});
            }
        }
    }

    for(int i = 2; i <= n; i ++) {
        fout << d[i] << ' ';
    }
    return 0;
}