Cod sursa(job #2556867)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 25 februarie 2020 11:38:15
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int MAXN = 1 * 1e5 + 5;
const int INF = 1e8;
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 dijkstra(){
    pq.push({1, 0});
    while(pq.size()){
        int d = pq.top().dest;
        int c = pq.top().cost;
        pq.pop();
        if(dist[d] == INF){
            dist[d] = c;
            for(auto x : g[d]){
                pq.push({x.dest, dist[d] + x.cost});
            }
        }
    }
}
int main()
{
    int n, m; fin >> n >> m;
    for(int i = 1; i <= n; ++i) dist[i] = INF;
    for(int i = 1; i <= m; ++i){
        int x, y, c; fin >> x >> y >> c;
        g[x].push_back({y, c});
    }
    dijkstra();
    for(int i = 2; i <= n; ++i) fout << dist[i] << " " ;
    return 0;
}