Cod sursa(job #3215885)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 15 martie 2024 13:59:37
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
typedef long long ll;
typedef pair<ll,ll> pll;
struct comp
{
    bool operator ()(pll a,pll b)
    {
        return a>b;
    }
};
const ll INF=1e16;
ll n,m;
vector<pll> muchii[50005];
bool use[50005];
ll dist[50005];
int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);
    fin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        ll a,b,c;
        fin>>a>>b>>c;
        muchii[a].push_back({b,c});
        muchii[b].push_back({a,c});
    }
    for(int i=1;i<=n;i++)
        dist[i]=INF;
    priority_queue<pll,vector<pll>,comp> pq;
    dist[1]=0;
    pq.push({0,1});
    while(!pq.empty())
    {
        ll nod=pq.top().second;
        pq.pop();
        if(use[nod])
            continue;
        use[nod]=1;
        for(pll i:muchii[nod])
            if(dist[i.first]>dist[nod]+i.second)
            {
                dist[i.first]=dist[nod]+i.second;
                pq.push({dist[i.first],i.first});
            }
    }
    for(int i=2;i<=n;i++)
    {
        if(dist[i]==INF)
            fout<<0<<' ';
        else
            fout<<dist[i]<<' ';
    }
    return 0;
}