Cod sursa(job #3318061)

Utilizator ianstefancazacuStefan-Cazacu Ian ianstefancazacu Data 26 octombrie 2025 19:51:35
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.64 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define inf 2000001

using namespace std;

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

struct edge{
    int node;
    int w;
};

vector <edge> graph[50001];


int prev1[50001];
int n,m;

int dist[50001];
bool visited[50001];

priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;

void read(){
    fin>>n>>m;
    for(int i=1; i<=m; i++){
        int node1, node2, w;
        fin>>node1>>node2>>w;
        edge edge_royale;
        edge_royale.node = node2;
        edge_royale.w = w;
        graph[node1].push_back(edge_royale);
    }
}

void diddy() {
    for (int i = 1; i <= n; i++){
        dist[i] = inf;
        prev1[i] = -1;
        visited[i] = false;
    }
    dist[1] = 0;
    pq.push({0, 1});
    while (!pq.empty()) {
        int diddler = pq.top().first;
        int u = pq.top().second;
        pq.pop();
        if (!visited[u]) {
            visited[u] = true;
            for (int i = 0; i < graph[u].size(); i++) {
                int victor = graph[u][i].node;
                int w = graph[u][i].w;
                if (dist[u] + w < dist[victor]) {
                    dist[victor] = dist[u] + w;
                    prev1[victor] = u;
                    pq.push({dist[victor], victor});
                }
            }
        }
    }
}

void out(){
    for(int i=2; i<=n; i++){
        if(dist[i] == inf) fout<<0<<" ";
        else fout<<dist[i]<<" ";
    }
}

void run(){
    read();
    diddy();
    out();
}

int main(){
    run();
    return 0;
}