Cod sursa(job #2388397)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 25 martie 2019 23:52:31
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 1e4 + 5;
const int ABC =  2 * 1e4 + 9;
struct edge{
    int dest, cost;
    bool operator < (const edge & aux) const{
        return cost > aux.cost;
    }
};
int dist[MAXN];
vector <edge> g[MAXN];
priority_queue <edge> pq;
void initializer(){
    for(int i = 1; i <= MAXN; ++i) dist[i] = ABC;
}
void ALG(int start){
    pq.push({start, 0});
    while(pq.size()){
        int node = pq.top().dest;
        int cost = pq.top().cost;
        pq.pop();
        if(dist[node] == ABC){
            dist[node] = cost;
            for(auto x : g[node])
                if(dist[x.dest] == ABC)
                    pq.push({x.dest, dist[node] + x.cost});
        }
    }
}

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int main()
{
    initializer();
    int n, m;
    fin >> n >> m;
    for(int i = 1; i <= m; ++i){
        int x, y, c;
        fin >> x >> y >> c;
        g[x].push_back({y, c});
    }
    ALG(1);
    for(int i = 2; i <= n; ++i){
        ///if(dist[i] == ABC) dist[i] = 0;
        fout << dist[i] << " ";
    }
    return 0;
}