Cod sursa(job #2793525)

Utilizator RaresLiscanLiscan Rares RaresLiscan Data 3 noiembrie 2021 18:32:23
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

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

struct varf {
    int nod, cost;
    bool operator < (const varf & b) const {
        return cost > b.cost;
    }
};

vector <varf> g[50005];
int v[50005];
priority_queue<varf> q;

void dijkstra(int s)
{
    q.push({s, 0});
    while (!q.empty()) {
        int currentNode = q.top().nod;
        int currentCost = q.top().cost;
        q.pop();
        for (auto i : g[currentNode]) {
            int cost = currentCost + i.cost;
            if (v[i.nod] > cost) {
                v[i.nod] = cost;
                q.push({i.nod, cost});
            }
        }
    }
}

int main()
{
    int n, m;
    fin >> n >> m;
    for (int i = 1; i <= m; i ++) {
        int a, b, c;
        fin >> a >> b >> c;
        g[a].push_back({b, c});
    }
    memset(v, INF, sizeof(v));
    dijkstra(1);
    for (int i = 2; i <= n; i ++) {
        if (v[i] >= INF) {
            fout << 0 << " ";
        }
        else {
            fout << v[i] << " ";
        }
    }
    return 0;
}