Cod sursa(job #3339115)

Utilizator nistor_dora_valentinaNistor Dora Valentina nistor_dora_valentina Data 6 februarie 2026 13:23:47
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <bits/stdc++.h>

using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, x1, y, c, i, dist[50005];
bool viz[50005];
struct nodd
{
    int nod, cost;
} x;
struct cmp
{
    bool operator()(const nodd&a, const nodd&b)
    {
        return a.cost>b.cost;
    }
};
priority_queue <nodd, vector <nodd>, cmp> q;
vector <nodd> v[50005];
int main()
{
    fin>>n>>m;
    for(i=1; i<=m; i++)
    {
        fin>>x1>>y>>c;
        v[x1].push_back({y, c});
    }
    q.push({1, 0});
    for(i=2; i<=n; i++)
        dist[i]=1e9;
    while(!q.empty())
    {
        x=q.top();
        q.pop();
        if(!viz[x.nod])
            for(auto i: v[x.nod])
                if(dist[i.nod]>dist[x.nod]+i.cost)
                {
                    dist[i.nod]=dist[x.nod]+i.cost;
                    q.push({i.nod, dist[i.nod]});
                }
        viz[x.nod]=1;
    }
    for(i=2; i<=n; i++)
        if(dist[i]==1e9)
            fout<<0<<" ";
    else
        fout<<dist[i]<<" ";
    return 0;
}