spfa模板加实例-刘汝佳紫书

主函数ac法 

Wormholes

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.. MW+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 backT 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

汉语题意:

在探索他的许多农场时,农夫约翰发现了许多令人惊奇的虫洞。虫洞是非常特殊的,因为它是一个单向的路径,把你送到它的目的地,在你进入虫洞之前的时间!FJ的每个农场包括N(1≤N≤500)块,方便编号为1。N, M(1≤≤2500)路径,和W W(1≤≤200)虫洞。


由于FJ是一个狂热的时间旅行爱好者,他想做以下事情:从某个领域开始,通过一些路径和虫洞旅行,并在他最初出发前回到起始领域。也许他能遇见他自己:)。


为了帮助FJ知道这是否可能,他会提供给你他农场F(1≤F≤5)的完整地图。任何路径的运行时间都不会超过1万秒,也没有虫洞可以让FJ回到超过1万秒的时间。


输入


第一行:一个整数F。F农场描述如下。

每个farm的第1行:三个空格分隔的整数:N、M和W

行2 . .每个农场的M+1:三个空格分隔的数字(S, E, T),分别描述:S和E之间的双向路径,需要T秒的时间。两个字段可能由多个路径连接。

行M + 2 . .每个农场的M+ W+1:三个空格分隔的数字(S, E, T),分别描述:从S到E的单向路径,也将旅行者向后移动T秒。


输出


行1 . .F:对于每个农场,如果FJ能够达到他的目标,输出“YES”,否则输出“NO”(不包含引号)。

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.

#include
#include
#include
#include 
#include
using namespace std;
const int MAXN = 1e5+5;
const int INF = 0x3f3f3f3f;
struct Edge
{
	int from,to,dist;
	Edge(int u,int v,int w):from(u),to(v),dist(w){	}
};
struct SPFA
{
	int n,m;
	vectoredges;
	vectorG[MAXN];
	bool vis[MAXN];
	int d[MAXN];
	int p[MAXN];
	int cnt[MAXN];
	void init(int n)
	{
		this->n = n;
		edges.clear();
		for(int i=0;i<=n;i++)G[i].clear();
	} 
	void AddEdge(int from,int to,int dist)
	{
		edges.push_back(Edge(from,to,dist));
		m = edges.size();
		G[from].push_back(m-1);
	}
	bool spfa(int s)
	{
		for(int i=0;i<=n;i++)d[i] = INF;
		memset(vis,0,sizeof(vis));
		memset(cnt,0,sizeof(cnt));
		d[s] = 0; vis[s] = true;
		queueQ;
		Q.push(s);
		while(!Q.empty())
		{
			int u = Q.front();
			Q.pop();
			vis[u] = false;
			for(int i=0;i d[u] +e.dist)
				{
					d[e.to] = d[u] + e.dist;
					p[e.to] = G[u][i];
					if(!vis[e.to])
					{
						Q.push(e.to);vis[e.to] = true;
						if(++cnt[e.to] > n)return false; 
					}
				 } 
			}
		}
		return true;
	}
	
}solve;//模板
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m,w;
		scanf("%d%d%d",&n,&m,&w);
		solve.init(n);
		while(m--)
		{
			int u,v,t;
			scanf("%d%d%d",&u,&v,&t);
			solve.AddEdge(u,v,t);
			solve.AddEdge(v,u,t);
		}
		while(w--)
		{
			int u,v,t;
			scanf("%d%d%d",&u,&v,&t);
			solve.AddEdge(u,v,-t);
		}
		if(!solve.spfa(1))printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}//写个主函数ac

 

你可能感兴趣的:(图论,模板)