Cod sursa(job #2232140)

Utilizator DandeacDan Deac Dandeac Data 17 august 2018 15:28:24
Problema Algoritmul Bellman-Ford Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

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

struct point
{
    int nod;
    int cost;
};

bool operator<(point a, point b)
{
    return a.cost > b.cost;
}

#define INF 0x3f3f3f3f
#define Gmax 50010

vector <pair <int, int> > G[Gmax];
priority_queue <point> pq;
int dist[Gmax];
int N,M;

int main()
{
    f>>N>>M;
    for(int i=1; i<=M; i++)
    {
        int x,y,c;
        f>>x>>y>>c;
        G[x].push_back(make_pair(y,c));
    }

    memset(dist, 0x3f, sizeof(dist));
    pq.push({1,0});


    while(!pq.empty())
    {
        int nod = pq.top().nod;
        int cost = pq.top().cost;
        pq.pop();

        if(dist[nod] != INF)
            continue;
        dist[nod] = cost;

        for(int i=0; i<G[nod].size(); i++)
        {
            pq.push({G[nod][i].first,cost + G[nod][i].second});
        }
    }
    for(int i=2; i<=N; i++)
    {
         if(dist[i]==INF) g<<0<<' ';
        else
            g<<dist[i] <<' ';
    }
    //g<<"Ciclu negativ!";
    return 0;
}