Cod sursa(job #2771325)

Utilizator stefanvoicaVoica Stefan stefanvoica Data 26 august 2021 16:17:13
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 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=100000000000000;

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

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

int32_t main()
{
    int i,m,x,y,z;
    fin>>n>>m;
    for (i=2;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;
}