Cod sursa(job #3347571)

Utilizator moloDaniMolodet Andrei Daniel moloDani Data 17 martie 2026 12:11:31
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

const int mxN = 5e4 + 1;

struct muchie{
    int vecin, cost;
};

bool operator>(muchie a, muchie b){
    return a.cost > b.cost;
}

int cost[mxN], n, m;
vector<muchie> G[mxN];
priority_queue<muchie, vector<muchie>, greater<muchie>> Q;

int main(){
    int a, b, c;
    fin >> n >> m;
    cost[1] = 1;

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

    Q.push({1, 1});

    while(!Q.empty()){
        muchie nod = Q.top();

        if(!cost[nod.vecin] || nod.vecin == 1){
            cost[nod.vecin] = nod.cost;

            for(auto x : G[nod.vecin]){
                if(cost[x.vecin] == 0){
                    Q.push({x.vecin, nod.cost + x.cost});
                }
            }
        }

        Q.pop();
    }

    for(int i = 2; i <= n; i++)
        fout << cost[i] - 1 << " ";
}