Cod sursa(job #3199778)

Utilizator indianu_talpa_iuteTisca Catalin indianu_talpa_iute Data 2 februarie 2024 17:46:08
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>

using namespace std;

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

int main() {
    int n, m;
    fin >> n >> m;
    std::vector<vector<pair<int, int>>> graph(n);

    for (int i = 0; i < m; i++) {
        int from, to, dist;
        fin >> from >> to >> dist;
        from--, to--;
        graph[from].emplace_back(dist, to);
    }

    std::vector<int> dists(n, INT_MAX);
    std::set<pair<int, int>> path;
    path.emplace( 0, 0 );
    while (!path.empty()) {
        pair<int, int> current = *path.begin();
        path.erase(path.begin());

        for (auto& neighbour: graph[current.second]) {
            if (dists[neighbour.second] == INT_MAX) {
                dists[neighbour.second] = current.first + neighbour.first;
                path.emplace(  current.first + neighbour.first, neighbour.second );
            }
        }
    }

    for (int i = 1; i < n; i++)
        fout << (dists[i] == INT_MAX ? 0 : dists[i]) << ' ';
    return 0;
}