Cod sursa(job #2165551)

Utilizator infomaxInfomax infomax Data 13 martie 2018 12:38:12
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

#define f first
#define s second

using namespace std;

ifstream F("dijkstra.in");
ofstream G("dijkstra.out");

int n, m, x, y, c, d[50005];
vector<pair<int, int> > a[50005];
priority_queue<pair<int, int> > pq;
bitset<50005> w;
const int inf=1<<30;

int main()
{
    F >> n >> m;
    for(int i = 1; i <= m; ++ i){
        F >> x >> y >> c;
        a[x].push_back({y, c});
    }
    pq.push({0, 1});
    for(int i = 2; i <= n; ++ i) d[i] = inf;
    while(!pq.empty()){
        x = pq.top().s;
        pq.pop();
        if(w[x]) continue;
        w[x] = 1;
        for(auto it:a[x])
            if(d[it.f] > d[x]+it.s)
                d[it.f] = d[x]+it.s, pq.push({-d[it.f], it.f});
    }
    for(int i = 2; i <= n; ++ i)
        if(d[i]==inf) G << 0 << " ";
        else G << d[i] << " ";
    return 0;
}