POJ3259 Wormholes【判断是否有负环】

Wormholes

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 65655   Accepted: 24488

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 Tseconds 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

问题链接:POJ3259 Wormholes

问题描述:FJ有F个农场,每个农庄里有N块地,有M条双向边:(S,E,T)表示S和E之间有一条双向边,需要T时间才能走完。

还有W个虫洞:(S,E,T)表示从S到E有一条单向边,从S到E会使得时间倒退T时间。问在每个农场里FJ能不能回到比开始还早的时间,如果可以输出YES,否则输出NO。

解题思路:实际上是求是否存在负环。

AC的C++程序:

Floyd算法判断是否有负环

#include<iostream>

using namespace std;
const int N=505;
const int INF=10000000;
int dp[N][N];

//floyd算法判断是否存在负环 
bool floyd(int n)
{
	for(int k=1;k<=n;k++)
	  for(int i=1;i<=n;i++){
	  	for(int j=1;j<=n;j++)
	  	  if(dp[i][j]>dp[i][k]+dp[k][j])
	  	    dp[i][j]=dp[i][k]+dp[k][j];
	  	if(dp[i][i]<0)
	  	  return true;
	  }
	return false;
}

int main()
{
	int F,N,M,W,a,b,t;
	scanf("%d",&F);
	while(F--){
		scanf("%d%d%d",&N,&M,&W);
		for(int i=1;i<=N;i++)
		  for(int j=1;j<=N;j++)
		    dp[i][j]=(i==j)?0:INF;
		while(M--){
			//双向边 
			scanf("%d%d%d",&a,&b,&t);
			if(t<dp[a][b])
			  dp[a][b]=dp[b][a]=t;
		}
		while(W--){
			//单向边
			scanf("%d%d%d",&a,&b,&t);
			dp[a][b]=-t;//负号表示使时间倒退-t 
		}
		if(floyd(N))
		  printf("YES\n");
		else
		  printf("NO\n"); 
	}
	return 0;
}

Bellman_ford算法判断是否有负环

#include<iostream>
#include<cstring>

using namespace std;

int dist[505];
const int INF=10000000;
struct Edge{
	int start,end,w;//从u到v有一条花费为w的路 
}edge[6000];

//n个结点 m条边 
bool Bellman_ford(int n,int m)
{
	memset(dist,INF,sizeof(dist));
	dist[1]=0;
	//进行n-1次松弛操作
	for(int i=1;i<n;i++)
	  for(int j=0;j<m;j++)
	    if(dist[edge[j].end]>dist[edge[j].start]+edge[j].w) 
	      dist[edge[j].end]=dist[edge[j].start]+edge[j].w;
	//判断是否存在负环
	for(int j=0;j<m;j++)
	   if(dist[edge[j].end]>dist[edge[j].start]+edge[j].w)
	     return true;
	return false;
}

int main()
{
	int F,N,M,W,a,b,t;
	scanf("%d",&F);
	while(F--){
		scanf("%d%d%d",&N,&M,&W);
		int k=0;
		while(M--){
			//双向边 
			scanf("%d%d%d",&a,&b,&t);
			edge[k].start=a,edge[k].end=b,edge[k].w=t;
			k++;
			edge[k].start=b,edge[k].end=a,edge[k].w=t;
			k++;
		}
		while(W--){
			//单向边
			scanf("%d%d%d",&a,&b,&t);
			edge[k].start=a,edge[k].end=b,edge[k].w=-t;
			k++; 
		}
		bool flag=Bellman_ford(N,k);
		printf("%s\n",flag?"YES":"NO"); 
	}
	return 0;
}

SPFA算法判断是否有负环:

#include<iostream>
#include<queue>
#include<vector>
 
using namespace std;
 
const int INF=0x3f3f3f3f;
const int N=505;
 
struct Edge{//边 
	int v,cost;//起点到终点v的花费为cost 
	Edge(int v,int cost):v(v),cost(cost){} 
};
 
int dist[N];//距离
bool vis[N];//标记结点是否进队列
int cnt[N];//标记结点进队列的次数 
vector<Edge>g[N];//图的邻接表表示
 
bool spfa(int n,int s)//n是结点的个数,s是源点 
{
	queue<int>q;
	for(int i=0;i<=n;i++){
		vis[i]=false;
		cnt[i]=0;
		dist[i]=INF;
	}
	vis[s]=true;
	dist[s]=0;
	cnt[s]=1;
	q.push(s);
	while(!q.empty()){
		int u=q.front();
		q.pop();
		vis[u]=false;//出队列标记为false 
		int num=g[u].size();//以u为起点的终点个数
		for(int i=0;i<num;i++){
			int v=g[u][i].v;//终点编号 
			if(dist[u]+g[u][i].cost<dist[v]){
				dist[v]=dist[u]+g[u][i].cost;
				if(!vis[v]){
					q.push(v);
					vis[v]=true;
					cnt[v]++;
					if(cnt[v]>=n)
					  return false;
				}
			}
		} 
	}
	return true;
}
 
int main()
{
	int T,n,m,w,a,b,t;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d",&n,&m,&w);
		for(int i=0;i<=n;i++)
		  g[i].clear(); 
		while(m--){
			scanf("%d%d%d",&a,&b,&t);
			g[a].push_back(Edge(b,t));
			g[b].push_back(Edge(a,t));
		}
		while(w--){
			scanf("%d%d%d",&a,&b,&t);
			g[a].push_back(Edge(b,-t));
		}
		int flag=spfa(n,1);
		if(flag)
		  printf("NO\n");
		else
		  printf("YES\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/83180742