Cod sursa(job #1179516)

Utilizator andreipurdilaAndrei Purdila andreipurdila Data 28 aprilie 2014 19:52:26
Problema Algoritmul Bellman-Ford Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <iostream>
#include <fstream>

using namespace std;

#define infinit 100000000;

int main()
{
    ifstream f("bellmanford.in");
    ofstream g("bellmanford.out");
    int n,m,*cost,i,j;
    struct M
    {
        int a,b,c;
    }muchie[100];
    f>>n;
    cost=new int [n+1];
    f>>m;
    for (i=1;i<=m;i++)
    {
        f>>muchie[i].a;
        f>>muchie[i].b;
        f>>muchie[i].c;
    }
    cost[1]=0;
    for (i=2;i<=n;i++)
        cost[i]=1000000;
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=m;j++)
            if (cost[muchie[j].a]+muchie[j].c<cost[muchie[j].b])
                cost[muchie[j].b]=cost[muchie[j].a]+muchie[j].c;
    }
    int ok=1;
    for (i=1;i<=m;i++)
        if (cost[muchie[i].a]+muchie[i].c<cost[muchie[i].b])
            ok=0;
    if (ok)
        for (i=1;i<=n;i++)
            cout<<cost[i]<<" ";
    else
        cout<<"Ciclu negativ";
    return 0;
}