Cod sursa(job #889834)

Utilizator the@EyE@Postavaru Stefan the@EyE@ Data 24 februarie 2013 18:33:57
Problema Algoritmul Bellman-Ford Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include<stdio.h>
#include<vector>
#include<algorithm>
#define INF 50002

using namespace std;

class edge
{
    public:int x,y,c;
};

vector<edge> edges;
int dist[INF],n,m;

int main()
{
    freopen("bellmanford.in","r",stdin);
    freopen("bellmanford.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;++i)
    {
        edge e;
        scanf("%d%d%d",&e.x,&e.y,&e.c);
        edges.push_back(e);
    }
    for(int i=1;i<=n;++i)dist[i]=99999999;
    bool ok=1;
    dist[1]=0;
    for(int i=0;ok&&i<n-1;++i)
    {
        ok=0;

        for(int j=0;j<edges.size();++j)
        {
            edge e=edges[j];
            if(dist[e.y]>dist[e.x]+e.c){dist[e.y]=dist[e.x]+e.c;ok=1;}
        }
    }
    for(int j=0;j<edges.size();++j)
        {
            edge e=edges[j];
            if(dist[e.y]>dist[e.x]+e.c){printf("Ciclu negativ!\n");return 0;}
        }
     for(int i=2;i<=n;++i)printf("%d ",dist[i]);
     return 0;
}