Cod sursa(job #3030232)

Utilizator brianna_enacheEnache Brianna brianna_enache Data 17 martie 2023 16:07:47
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
#define nmax 250005
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({cost, y});
    }
    for(i = 1; i <= n; i++)
        d[i] = 1e9;
    int k;
    d[1] = 0;
    q.push({0, 1});
    while(!q.empty())
    {
        k = q.top().second;
        q.pop();
        if(viz[k] == 0)
        {
            viz[k] = 1;
            for(auto e : L[k])
            {
                if(d[e.second] > d[k] + e.first)
                   d[e.second] = d[k] + e.first;
                q.push({-d[e.second], e.second});
            }
        }
    }
    for(i = 2; i <= n; i++)
        if(d[i] != 1e9) out << d[i] << " ";
        else out << 0 << " ";
    return 0;
}