Cod sursa(job #1793763)

Utilizator paulstepanovStepanov Paul paulstepanov Data 31 octombrie 2016 15:30:58
Problema Algoritmul Bellman-Ford Scor 15
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");

const int NMax = 50005;
const int oo = 1000000000;
int N,M,D[NMax];
vector < pair <int, int> > G[NMax];


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

int BellmanFord()
{
  for(int i = 2; i <= N; ++i)
    D[i] = oo;

  for(int k = 1 ; k <= N; ++k)
    {
      for(int i = 1; i <= N; ++i)
        for(int j = 0; j < (int) G[i].size(); ++j)
          {
            int Vecin = G[i][j].first, Cost = G[i][j].second;
            D[Vecin] = min(D[Vecin],D[i] + Cost);
          }
    }
  return 0;
}

void Print()
{
  for(int i = 2; i <= N ; ++i)
    fout<<D[i]<<" ";
  fout<<"\n";
}

int main()
{
    Read();
    if(BellmanFord()==-1)
      fout<<"Ciclu negativ!\n";
    else
      Print();
    return 0;
}