Cod sursa(job #2573085)

Utilizator Groza_Iulia_DianaGroza Iulia Diana Groza_Iulia_Diana Data 5 martie 2020 15:44:38
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({-dist, y});
    }
    Q.push({0, 1});
    for(int i=2; i<=n; i++)
    {
        d[i]=-INF;
        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.first+d[x]>d[i.second])
                {
                    Q.push({i.first+d[x], i.second});
                    d[i.second]=i.first+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;
}