Cod sursa(job #2766249)

Utilizator stefanvoicaVoica Stefan stefanvoica Data 31 iulie 2021 14:11:41
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>
#define mod 1000000007
#define int long long
#define dim 50002
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout("dijkstra.out");
int n,sol[dim];
const int inf=1000000000000000;

struct el
{
    int nod,cost;
    bool operator < (const el &A) const
    {
        return cost>A.cost;
    }
};
priority_queue<el> pq;

struct elem
{
    int dest,c;
};
vector<elem> a[dim];

void dijkstra ()
{
    while (pq.empty()==0)
    {
        el x=pq.top();
        pq.pop();
        for (auto y: a[x.nod])
                if (sol[y.dest]==inf)
                {
                    sol[y.dest]=x.cost+y.c;
                    pq.push({y.dest,sol[y.dest]});
                }
    }
}

int32_t main()
{
    int i,m,x,y,z;
    fin>>n>>m;
    for (i=1; i<=n; i++)
        sol[i]=inf;
    for (i=1; i<=m; i++)
    {
        fin>>x>>y>>z;
        a[x].push_back({y,z});
    }
    pq.push ({1,0});
    dijkstra();
    for (i=2; i<=n; ++i)
        if (sol[i]==inf)
            fout<<0<<' ';
        else    fout<<sol[i]<<' ';
    return 0;
}