Cod sursa(job #2567299)

Utilizator T_george_TGeorge Teodorescu T_george_T Data 3 martie 2020 16:31:16
Problema Algoritmul Bellman-Ford Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("bellmanford.in");
ofstream out("bellmanford.out");
#define NMAX 50001
struct edge{
    int dest,cost;
    bool operator <(const edge&aux)const{
    return cost>aux.cost;
    }
}aux;
vector<edge>g[NMAX];
priority_queue<edge>pq;
int n,m,dist[NMAX],seen[NMAX];
void dijkstra(){
    seen[1]=1;
    aux.cost=0;
    aux.dest=1;
    pq.push(aux);
    while(!pq.empty()){
        int cost=pq.top().cost;
        int node=pq.top().dest;
            seen[node]=1;
        pq.pop();
            dist[node]=cost;
            for(auto y:g[node]){
                if(!seen[y.dest]){
                aux.cost=cost+y.cost;
                aux.dest=y.dest;
                pq.push(aux);
                }
            }
        }
    }

int main()
{
    in>>n>>m;
    int x,y,cost;
    for(int i=1;i<=m;i++){
        in>>x>>y>>cost;
        aux.dest=y;
        aux.cost=cost;
        g[x].push_back(aux);
    }
    dijkstra();
    for(int i=2;i<=n;i++){
        if(dist[i]==-1)
            out<<0<<" ";
        else
            out<<dist[i]<<" ";
    }
    return 0;
}