POJ 3259:Wormholes:bellman_ford算法判负环

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 29928   Accepted: 10832

Description

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..NM (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input

Line 1: A single integer,  FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively:  NM, and  W 
Lines 2.. M+1 of each farm: Three space-separated numbers ( SET) that describe, respectively: a bidirectional path between  S and  E that requires  T seconds to traverse. Two fields might be connected by more than one path. 
Lines  M+2.. M+ W+1 of each farm: Three space-separated numbers ( SET) that describe, respectively: A one way path from  S to  E that also moves the traveler back  T seconds.

Output

Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

Hint

For farm 1, FJ cannot travel back in time. 
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

Source

USACO 2006 December Gold
这道题目是有向图判断是否有总和是负值的环存在。
原本的思路是,跟判断环是否存在一样,使用深搜遍历加剪枝。存储结构使用邻接表。邻接表的使用方法:
typedef struct node
{
	int number;   //被指向的节点
	int cost;     //路径的花费
	struct node* next;   //下一个节点的指针
}node;
node a[1000];         //开始的节点
void charu(int x,int y,int cos) //将一条由x指向y的花费cos的路径插入邻接表
{
	int i,j;
	node* p=(node*)malloc(sizeof(node));
	p->cost=cos;
	p->number=y;
	p->next=a[x].next;
	a[x].next=p;
}

但是,这种方式要针对每一个节点进行遍历,即使剪枝,时间复杂度太高,超时。
这道题目是标准的bellman_ford最短路算法的应用。Dijkstra算法无法判断含负权边的图的最短路。如果遇到负权,在没有负权回路存在时(负权回路的含义是,回路的权值和为负。)即便有负权的边,也可以采用Bellman-Ford算法正确求出最短路径。
bool Bellman-Ford(G,w,s) //图G ,边集 函数 w ,s为源点
for each vertex v ∈ V(G) //初始化 1阶段
d[v] ←+∞;
d[s] ←0; //1阶段结束
for(int i=1;i<|v|;i++) //2阶段开始,双重循环。
for each edge(u,v) ∈E(G) //边集数组要用到,穷举每条边。
if(d[v]> d[u]+ w(u,v))//松弛判断
d[v]=d[u]+w(u,v); //松弛操作2阶段结束
for each edge(u,v) ∈E(G)
if(d[v]> d[u]+ w(u,v))
return false;
return true;
如果在某一遍迭代中,松弛操作未执行,说明该遍迭代所有的边都没有被松弛。至此后,边集中所有的边都不需要再被松弛,从而可以提前结束迭代过程。优化的措施就非常简单。 优化后的算法在处理有负权回路的测试数据时,由于每次都会有边被松弛,因而不可能提前终止外层循环。这对应了最坏情况,其时间复杂度仍旧为O(VE)。
优化后的算法的时间复杂度已经和用二叉堆优化的Dijkstra算法相近了,而编码的复杂程度远比后者低。加之Bellman-Ford算法能处理各种边值权情况下的最短路径问题,因而还是非常优秀的。

使用初步优化的bellman_ford算法求解该问题:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 10005
int f,n,m,w,s,e,t,result,enumber;
typedef struct node
{
	int x,y,cost;
}node;
node a[5300];
int d[505];
void init()
{
	int i,j,k;
	scanf("%d%d%d",&n,&m,&w);
	k=0;
	for(i=0;i<m;i++)
	{
		scanf("%d%d%d",&s,&e,&t);
		a[k+1].y=a[k].x=s;
		a[k+1].x=a[k].y=e;
		a[k+1].cost=a[k].cost=t;
		k+=2;
	}
	for(i=0;i<w;i++)
	{
		scanf("%d%d%d",&s,&e,&t);
		a[k].x=s;
		a[k].y=e;
		a[k].cost=-1*t;
		k++;
	}
	enumber=k;
	result=0;
	for(i=0;i<=n;i++)
		d[i]=MAXN;
}
void bellman_ford()
{
	int i,j;
	int p;
	d[1]=0;
	for(i=0;i<n-1;i++)
	{
		p=1;
		for(j=0;j<enumber;j++)
		{
			if(d[a[j].x]+a[j].cost<d[a[j].y])
			{
				d[a[j].y]=d[a[j].x]+a[j].cost;
				p=0;
			}
		}
		if(p)
			break;
	}
	for(j=0;j<enumber&&!result;j++)
		if(d[a[j].x]+a[j].cost<d[a[j].y])
			result=1;
}
int main()
{
	int i,j;
	scanf("%d",&f);
	while(f--)
	{
		init();
		bellman_ford();
		if(result)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}



你可能感兴趣的:(POJ 3259:Wormholes:bellman_ford算法判负环)