Cod sursa(job #2864448)

Utilizator MacaroaneFierteSimandan Paul MacaroaneFierte Data 7 martie 2022 21:35:16
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

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

vector < pair < int, int > > v[50001];
int n, m, c, x, y, st, oo = 20000000, d[50001];


void dijk() {
    for (int i = 2; i <= n; i++)
        d[i] = oo;
    queue < int > q;
    q.push(1);
    while (!q.empty()) {
        int a = q.front();
        q.pop();
        for (auto i : v[a]) {
            int b = i.first, cost = i.second;
            if (d[b] > d[a] + cost) {
                d[b] = d[a] + cost;
                q.push(b);
            }
        }
    }
    for (int i = 2; i <= n; i++)
        if (d[i] == oo)
            out << "0 ";
        else
            out << d[i] << ' ';
}

int main() {
    in >> n >> m;
    for (int i = 1; i <= m; i++) {
        in >> x >> y >> c;
        v[x].push_back({y, c});
    }
    dijk();
    return 0;
}