Wormholes (spfa和bellman-ford板子题)

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

扫描二维码关注公众号,回复: 8614216 查看本文章

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.

spfa算法还是比较容易搞懂的。

在一个图n个点,从一个点如果想要回到自己,最多可以经过n-1个点,经过n条边。

SPFA:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=1000+10;
const int maxm=5500+10;
const int inf=0x3f3f3f3f;
struct Edge
{
	int before;
	int to;
	int w;
}e[maxm];
int head[maxn],dis[maxn],num[maxn],vis[maxn];
int n,m,w,a,b,c,k;
void add(int u,int v,int w)
{
	e[k].to=v;
	e[k].w=w;
	e[k].before=head[u];
	head[u]=k++;
}
bool Spfa()
{
	memset(num,0,sizeof num);
	memset(vis,0,sizeof vis);
	memset(dis,0x3f,sizeof dis);
	vis[1]=num[1]=1;
	dis[1]=0;
	queue<int> q;
	q.push(1); 
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i!=-1;i=e[i].before)
		{
			int v=e[i].to;
			if(dis[v]>dis[u]+e[i].w)
			{
				dis[v]=e[i].w+dis[u];
				if(!vis[v])
				{
					num[v]++;
					if(num[v]>=n)
						return false;
					vis[v]=1;
					q.push(v);
				}	
			}
		}
	}
	return true;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>w;
		k=0;
		memset(head,-1,sizeof head);
		for(int i=0;i<m;i++)
		{
			cin>>a>>b>>c;
			add(a,b,c);
			add(b,a,c);
		}
		for(int i=0;i<w;i++)
		{
			cin>>a>>b>>c;
			add(a,b,-c);
		}
		if(Spfa())
		{
			cout<<"NO"<<"\n";
		}
		else
			cout<<"YES"<<"\n";
	}
	return 0;
}  

Bellman-Ford:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000+10;
const int maxm=5500+10;
const int inf=0x3f3f3f3f;
struct Edge
{
	int before;
	int to;
	int w;
}e[maxm];
int n,m,w,a,b,c,k,head[maxn],dis[maxn];
void add(int u,int v,int w)
{
	e[k].to=v;
	e[k].w=w;
	e[k].before=head[u];
	head[u]=k++;
}
bool Bellman_Ford()
{
	memset(dis,0x3f,sizeof dis);
	dis[1]=0;
	for(int i=0;i<n-1;i++)  // 外循环次数 n-1 次 
	{
		for(int j=1;j<=n;j++)  // 对每个点 
		{
			if(dis[j]==inf)
			continue;
			for(int k=head[j];k!=-1;k=e[k].before) // 对每条边
			{
				if(e[k].w!=inf&&dis[e[k].to]>dis[j]+e[k].w)
				{
					dis[e[k].to]=dis[j]+e[k].w;
				}
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		if(dis[i]==inf)
		continue;
		for(int j=head[i];j!=-1;j=e[j].before)
		{
			if(e[j].w!=inf&&dis[e[j].to]>dis[i]+e[j].w)
			return false;
		}
	}
	return true;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>m>>w;
		k=0;
		memset(head,-1,sizeof head);
		for(int i=0;i<m;i++)
		{
			cin>>a>>b>>c;
			add(a,b,c);
			add(b,a,c);
		}
		for(int i=0;i<w;i++)
		{
			cin>>a>>b>>c;
			add(a,b,c*(-1));
		}
		if(Bellman_Ford())
		{
			cout<<"NO"<<"\n";
		}
		else
			cout<<"YES"<<"\n";
	}
	return 0;
} 
发布了51 篇原创文章 · 获赞 21 · 访问量 3082

猜你喜欢

转载自blog.csdn.net/qq_44115065/article/details/102999802