Cod sursa(job #2280226)

Utilizator RAZVAN_NISTORNistor Razvan RAZVAN_NISTOR Data 10 noiembrie 2018 12:50:17
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("abc.in");
ofstream g("abc.out");
const int INF=1e9;
const int Nmax=5e4+9;
vector< pair<int,int> > G[Nmax];
int d[Nmax];
void dijsktra()
{
    priority_queue <pair<int,int> ,vector <pair <int,int> >,greater< pair <int,int > > >pq;
    pq.push({0,1});
    while(!pq.empty())
    {
        int node=pq.top().second;
        int val=pq.top().first;
        pq.pop();
        if(d[node]!=val)continue;
        for(auto x: G[node])
          {
              if(val+x.second<d[x.first])
               {
                    d[x.first]=val+x.second;
                pq.push({d[x.first],x.first});
               }
          }

    }

}
int main()
{
    int n,m;
    f>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        f>>a>>b>>c;
        G[a].push_back({b,c});
    }
    for(int i=2;i<=n;i++)d[i]=INF;
    dijsktra();
    for(int i=2;i<=n;i++)
        if(d[i]==INF)
            g<<"0 ";
        else
            g<<d[i]<<" ";
    return 0;
}