POJ-3259(bellman-ford判断有没有负权路)

算法导论362页,例子

#define EDGE 2505 * 4
#define N 505 * 2
struct EDGE_
{
	int x, y;
	int value;
} e[EDGE];
int n, edge;//n是顶点数,m是边数
int d[N + 5];
/*
2 
5 7 3 
1 2 6 
1 3 7 
2 4 5 
2 3 8 
3 5 9 
5 1 2 
5 4 7 
2 5 4 
4 2 2 
3 4 3 
*/
bool bellman_ford(int s)
{
	int i, j, ii;
	for (i = 1; i <= n; ++i)
		d[i] = MY_INT_MAX;
	d[s] = 0;
	for (i = 1; i < n; ++i) {
		for (j = 0; j < edge; ++j) {
			if (d[e[j].x] != MY_INT_MAX && d[e[j].x] + e[j].value < d[e[j].y]) {
				d[e[j].y] = d[e[j].x] + e[j].value;
			}
		}
		/*
		for (ii = 1; ii <= n; ++ii) {
			printf("%d ", d[ii]);
		}
		printf("\n");*/
	}
	for (j = 0; j < edge; ++j) {
		if (d[e[j].x] + e[j].value < d[e[j].y]) {
			return false;
		}
	}
	return true;
}


你可能感兴趣的:(algorithm(图论),struct,算法)