POJ3259 Wormholes【SPFA】

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.

解题思路

题目大意:有一个人有n块农田和m条连接农田的道路,走每段路都需要花费一定的时间,同时他还非常热衷于时间穿梭。他想要到达某一块农田后能够通过虫洞穿越回他从起点出发之前的时间。如果他能够实现这个愿望就输出YES,否则就输出NO。

思路:本题其实是一个 spfa 判负环的模板题,如果能有一个负环的存在,那么肯定证明他可以穿越回到那个虫洞能带他去的位置出发之前的时候。这里我们使用链式前向星建图,然后使用SPFA判断负环,当出现负环返回0,没有负环则返回1。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
#define MAXN 3000

int n, m, w;
int num[MAXN], vis[MAXN], dist[MAXN];
int head[MAXN];
int cnt = 0;

struct Node {
	int w;
	int to;
	int next;
}edge[MAXN];

void add(int u, int v, int w)
{
	edge[cnt].w = w;
	edge[cnt].to = v;
	edge[cnt].next = head[u];
	head[u] = cnt++;
}

int spfa()
{
	queue<int> myque;
	memset(num, 0, sizeof(num));
	memset(dist, inf, sizeof(dist));
	memset(vis, 0, sizeof(vis));
	vis[1] = 1;
	dist[1] = 0;
	num[1] ++;
	myque.push(1);
	while (!myque.empty())
	{
		int q = myque.front();
		myque.pop();
		vis[q] = 0;
		for (int i = head[q]; ~i; i = edge[i].next)
		{
			int to = edge[i].to;
			if (dist[q] + edge[i].w < dist[to])   //如果该点能被更新
			{
				dist[to] = dist[q] + edge[i].w;
				if (!vis[to])
				{
					num[to]++;
					if (num[to] >= n)
						return 0;
					vis[to] = 1;
					myque.push(to);
				}
			}
		}
	}
	return 1;
}

int main()
{
	int F;
	scanf("%d", &F);
	while (F--)
	{
		cnt = 0;
		scanf("%d%d%d", &n, &m, &w);
		memset(head, -1, sizeof(head));
		for (int i = 0; i < m; i++)
		{
			int u, v, t;
			scanf("%d%d%d", &u, &v, &t);
			add(u, v, t);
			add(v, u, t);
		}
		for (int i = 0; i < w; i++)     //存虫洞,单向且按照负权存
		{
			int u, v, t;
			scanf("%d%d%d", &u, &v, &t);
			add(u, v, t*(-1));
		}
		if (spfa())
			printf("NO\n");
		else printf("YES\n");
	}
	return 0;
}

 

posted @ 2020-06-19 22:55  Hu_YaYa  阅读(16)  评论(0)    收藏  举报