Wormholes(最短路)

Wormholes(最短路)

Time limit:2000 ms
Memory limit:65536 kB
OS:Linux
judge:https://vjudge.net/contest/297882#problem/F

描述

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…N, M (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, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2… M+1 of each farm: Three space-separated numbers ( S, E, T) 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 ( S, E, T) 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).

题意

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

因为FJ是一个狂热的时间旅行迷,他想做以下的事情:从某个领域开始,穿过一些路径和虫洞,在他最初离开之前的一段时间回到起始领域。也许他能认识自己:)。

为了帮助FJ了解这是否可行,他将向您提供其农场F(1≤F≤5)的完整地图。任何路径的运行时间都不会超过10000秒,任何虫洞都不能使FJ及时返回10000秒以上。

输入

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

每个农场的第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能够实现他的目标,输出“是”,否则输出“否”(不包括报价)。

提示

对于1号农场,FJ不能及时返回。

对于农场2,FJ可以通过循环1->2->3->1及时返回,在他离开前1秒钟回到起始位置。他可以从循环的任何地方开始来完成这个任务。

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.

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define maxn 505
#define inf 0x3f3f3f3f
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
struct poi {
	int v, w;
	poi() {}
	poi(int v, int w) :v(v), w(w) {}
	bool operator < (const poi tem) const {
		return w < tem.w;
	}
};
int N = 0, M = 0, W = 0;
int dis[maxn];
int cnt[maxn];
bool vis[maxn];
vector<poi> mp[maxn];
bool SPFA(int S) {
	memset(dis, 0x3f, sizeof(dis));
	memset(vis, 0, sizeof(vis));
	memset(cnt, 0, sizeof(cnt));

	dis[S] = 0;
	_for(q, N) {
		_rep(i, 1, N) {
			_for(j, mp[i].size()) {
				if (dis[mp[i][j].v] > dis[i] + mp[i][j].w) {
					dis[mp[i][j].v] = dis[i] + mp[i][j].w;
				}
			}
		}
	}
	_rep(i, 1, N) {
		_for(j, mp[i].size()) {
			if (dis[mp[i][j].v] > dis[i] + mp[i][j].w) {
				return 1;
			}
		}
	}
	return 0;
}
int main() {
	//freopen("in.txt", "r", stdin);
	int F;
	scanf("%d", &F);
	while (F--) {
		_rep(i, 1, N) mp[i].clear();
		scanf("%d%d%d", &N, &M, &W);
		_for(i, M) {
			int S, E, T;
			scanf("%d%d%d", &S, &E, &T);
			mp[S].push_back(poi(E, T));
			mp[E].push_back(poi(S, T));
		}
		_for(i, W) {
			int S, E, T;
			scanf("%d%d%d", &S, &E, &T);
			mp[S].push_back(poi(E, -T));
		}
		printf("%s", SPFA(1) ? "YES\n" : "NO\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值