Cod sursa(job #3030123)

Utilizator brianna_enacheEnache Brianna brianna_enache Data 17 martie 2023 15:26:56
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
#define nmax 250005
#define oo 1e9
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
vector <pair<int, int>>L[nmax];
priority_queue< pair<int, int>> q;
int n, m, viz[50005], d[50005];
int main()
{
    int i, j, x, y, cost;
    in >> n >> m;
    for(i = 1; i <= m; i++)
    {
        in >> x >> y >> cost;
        L[x].push_back({y, cost});
        ///L[y].push_back({x,cost});
    }
    for(i = 1; i <= n; i++)
    {
        d[i] = oo;
        viz[i] = 0;
    }
    int k;
    d[1] = 0;
    q.push({1, 0});
    while(!q.empty())
    {
        k = q.top().first;
        q.pop();
        if(viz[k] == 0)
        {
            viz[k] = 1;
            for(auto e : L[k])
            {
                if(d[e.first] > d[k] + e.second)
                   d[e.first] = d[k] + e.second;
                q.push({e.first, -d[e.first]});
            }
        }
    }
    for(i = 2; i <= n; i++)
        if(d[i] != oo) out << d[i] << " ";
        else out << 0 << " ";
    return 0;
}