Cod sursa(job #2594400)

Utilizator Groza_Iulia_DianaGroza Iulia Diana Groza_Iulia_Diana Data 5 aprilie 2020 21:50:37
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.05 kb
#include <bits/stdc++.h>
#define nMax 50005

using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

const int INF=1e9;
int n, m, x, y, dist, d[nMax];
vector<pair<int, int>> G[nMax];
priority_queue<pair<int, int>> Q;
bitset<nMax> viz;

int main()
{
    fin >> n >> m;
    while(m--)
    {
        fin >> x >> y >> dist;
        G[x].push_back({y, dist});
    }
    Q.push({0, 1});
    for(int i=2; i<=n; i++)
    {
        d[i]=-1e9;
        Q.push({d[i], i});
    }
    while(!Q.empty())
    {
        if(!viz[Q.top().second])
        {
            int x=Q.top().second;
            for(auto i:G[x])
                if(-i.second+d[x]>d[i.first])
                {
                    Q.push({-i.second+d[x], i.first});
                    d[i.first]=-i.second+d[x];
                }
            viz[x]=1;
        }
        else
            Q.pop();
    }
    for(int i=2; i<=n; i++)
        if(d[i]!=-INF)
            fout << -d[i] << " ";
        else
            fout << "0 ";
    return 0;
}