Cod sursa(job #3238618)

Utilizator stefanrotaruRotaru Stefan-Florin stefanrotaru Data 28 iulie 2024 12:05:04
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

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

int n, m;

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

void dijkstra()
{

}

int main()
{
    f >> n >> m;

    vector <pair <int, int> > a[n + 5];

    vector <bool> viz(n + 5, 0);

    vector <int> cost(n + 5, 1e9);

    for (int i = 1; i <= m; ++i) {
        int x, y, z;

        f >> x >> y >> z;

        a[x].push_back({y, z});
        ///a[y].push_back({x, z});
    }

    dijkstra();

    cost[1]  = 0;

    q.push({0, 1});

    while (!q.empty()) {
        int costu = q.top().first;
        int nod = q.top().second;

        q.pop();

        if (viz[nod]) {
            continue;
        }

        viz[nod] = 1;

        for (auto vecin : a[nod]) {
            int val = vecin.second;
            int next = vecin.first;

            if (costu + val < cost[next]) {
                cost[next] = val + costu;
                q.push({cost[next], next});
            }
        }
    }

    for (int i = 2; i <= n; ++i) {
        if (cost[i] == 1e9) {
            cost[i] = 0;
        }

        g << cost[i] << ' ';
    }

    return 0;
}