Cod sursa(job #2963963)

Utilizator Latyn76Tinica Alexandru Stefan Latyn76 Data 12 ianuarie 2023 10:48:35
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, dp[50552], viz[50005];
const int INF = 1e9;
vector<pair<int,int>>a[50002];
priority_queue<pair<int,int>>q;
void Djk(int start)
{
    for(int i = 1; i <= n; i++)
        dp[i] = INF;
    dp[start] = 0;
    q.push({0,start});
    viz[start] = 1;
    while(!q.empty())
    {
        int nod = q.top().second;
        q.pop();
        for(auto w : a[nod])
            if(!viz[w.first])
        {
            if (dp[nod] + w.second < dp[w.first])
            {
                dp[w.first] = dp[nod] + w.second;
                q.push({-w.second, w.first});
                viz[w.first] = 1;
            }
        }
    }
}

int main()
{
    fin >> n >> m;
    int x, y, c;
    for (int i = 1; i <= m; i++)
    {
        fin >> x >> y >> c;
        a[x].push_back({y, c});
        //a[y].push_back({x, c});
    }
    Djk(1);
    for (int i = 2; i <= n; i++)
        if(dp[i] != INF)
        fout << dp[i] << ' ';
        else fout <<"0 ";
    fout << '\n';
    return 0;
}