Cod sursa(job #523197)

Utilizator gabipurcaruGabi Purcaru gabipurcaru Data 17 ianuarie 2011 13:19:06
Problema Algoritmul Bellman-Ford Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
// infoarena: problema/bellmanford //
#include <fstream>
using namespace std;

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

const int MAXM = 250010;
const int MAXN = 50010;

int n,m,i,j,k,e[MAXM][3],d[MAXN],nod,muchie,c,ok;

int main()
{
	in>>n>>m;
	for(i=1; i<=m; i++)
		in>>e[i][0]>>e[i][1]>>e[i][2];
	
	for(i=2; i<=n; i++)
		d[i] = 1<<29;
	
	for(nod=2; nod<=n; nod++)
	{
		for(muchie=1; muchie<=m; muchie++)
		{
			i = e[muchie][0];
			j = e[muchie][1];
			c = e[muchie][2];
			if(d[j] > d[i] + c)
				d[j] = d[i] + c, ok = 1;
		}
	}
	for(muchie=1; muchie<=m; muchie++)
	{
		i = e[muchie][0];
		j = e[muchie][1];
		c = e[muchie][2];
		if(d[j] > d[i] + c)
		{
			out<<"Ciclu negativ!";//<<i<<' '<<j;
			return 0;
		}
	}
	
	for(i=2; i<=n; i++)
		out<<d[i]<<' ';
	
	return 0;
}