Cod sursa(job #2420920)

Utilizator bluestorm57Vasile T bluestorm57 Data 13 mai 2019 16:20:44
Problema Algoritmul lui Dijkstra Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("dijkstra.in");
ofstream g("dijkstra.out");

const int NMAX = 50005;
const int inf = 1e9;
struct muchie{
    int node, cost;
};
priority_queue< pair<int,int> > q;
vector <pair <int,int > > v[NMAX];
int n,m,dist[NMAX];
bool viz[NMAX];

int main(){
    int i,a,b,c;
    muchie x;
    f >> n >> m;
    for(i = 1 ; i <= m ; i++){
        f >> a >> b >> c;
        v[a].push_back({b,c});
    }

    for(i = 2 ; i <= n ; i++)
        dist[i] = inf;

    q.push({0,1});
    while(!q.empty()){
        int nod = q.top().second;
        q.pop();
        if(viz[nod])
            continue;
        for(i = 0 ; i < v[nod].size() ; i++){
            int value = dist[nod] + v[nod][i].second;
            if(dist[v[nod][i].first] > value){
                dist[v[nod][i].first] = value;
                q.push({-dist[v[nod][i].first],v[nod][i].first});
            }
        }
    }

    for(i = 2 ; i <= n ; i++)
        if(dist[i] == inf)
            g << 0 << " ";
        else
            g << dist[i] << " ";


return 0;
}