Cod sursa(job #3304895)

Utilizator robertcosacCosac Robert-Mihai robertcosac Data 28 iulie 2025 13:42:44
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct elem
{
    int nod, cost;
    bool operator < (const elem & other) const
    {
        return cost>other.cost;
    }
};
priority_queue <elem> pq;
vector <elem> v[50009];
int n, sol[50009];
void dijkstra ()
{
    while (!pq.empty())
    {
        elem x=pq.top();
        pq.pop();
        if (x.cost==sol[x.nod])
        {
            for (auto y:v[x.nod])
            {
                if (x.cost+y.cost<sol[y.nod])
                {
                    sol[y.nod]=x.cost+y.cost;
                    pq.push({y.nod, sol[y.nod]});
                }
            }
        }
    }
}
signed main ()
{
    int m;
    f >> n >> m;
    for (int i=1; i<=m; i++)
    {
        int x, y, z;
        f >> x >> y >> z;
        v[x].push_back({y,z});
    }
    for (int i=2; i<=n; i++)
        sol[i]=1e9;
    pq.push({1, 0});
    dijkstra ();
    for (int i=2; i<=n; i++) g << sol[i] << ' ';
}