Cod sursa(job #2989214)

Utilizator brianna_enacheEnache Brianna brianna_enache Data 6 martie 2023 10:54:03
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
#define nmax 250005
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");

vector <pair <int, int>> a[nmax];
priority_queue <pair <int, int>> q;
int viz[50005], d[50005];
int n, m;

void Dij(int s)
{
    d[s] = 0;
    q.push({0, s});
    int x;
    while(!q.empty())
    {
        x = q.top().second;
        q.pop();
        if(viz[x] == 0)
        {
            viz[x] = 1;
            for(auto i : a[x])
            {
                if(d[i.second] > d[x] + i.first)
                   d[i.second] = d[x] + i.first;
                q.push({-d[i.second], i.second});
            }
        }
    }
}
int main()
{
    int i, x, y, c;
    in >> n >> m;
    for(i = 1; i <= m; i++)
    {
        in >> x >> y >> c;
        a[x].push_back({c, y});
    }
    for(i = 1; i <= n; i++)
        d[i] = 1e9;
    Dij(1);

    for(i = 2; i <= n; i++)
        if(d[i] != 1e9) out << d[i] << " ";
        else out << 0 << " ";
    return 0;
}