博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Wormholes
阅读量:5806 次
发布时间:2019-06-18

本文共 3910 字,大约阅读时间需要 13 分钟。

Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit

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..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).

Sample Input

23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8

Sample Output

NOYES

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.
意思就是看看从一个点出发经过多个边权值(肯定有负的,回到自身的时候是不是负的,那样就是时光倒流了,本题是看1那点,能不能时光倒流
题目大意:
农民约翰在农场散步的时候发现农场有大量的虫洞,这些虫洞是非常特别的因为它们都是单向通道,为了方便现在把约翰的农田划分成N快区域,M条道路,W的虫洞。
约翰是时空旅行的粉丝,他希望这样做,在一个区域开始,经过一些道路和虫洞然后回到他原来所在的位置,这样也许他就能见到他自己了。
穿越虫洞会回到以前。。。。。(穿越者约翰)
/
很明显这是一个很扯淡的故事,不过为了出一道带负权值的题目也是难为了出题人,迪杰斯特拉算法不能处理带有负权值的问题,佛洛依德倒是可以,不过复杂度很明显太高,所以spfa是不错的选择,只需要判断出发点时间是否变小.
#include
#include
#include
using namespace std;#define N 550#define INF 0xffffffstruct node{ int y, w;}P[N];vector
G[N];int v[N];int spfa(int s){ queue
Q; Q.push(s); while(Q.size()) { s = Q.front(); Q.pop(); int len = G[s].size(); for(int i = 0; i < len; i++) { node q = G[s][i]; if(v[s] + q.w < v[q.y]) { v[q.y] = v[s] + q.w; Q.push(q.y); } } if(v[1] < 0) return 1; } return 0;}int main(){ int t, n, m, w, s, e, f; cin >> f; while(f--) { for(int i = 1; i < N; i++) { v[i] = INF; G[i].clear(); } v[1] = 0; cin >> n >> m >> w; node p; for(int i = 0; i < m; i++) { cin >> s >> e >> t; p.y = e, p.w = t; G[s].push_back(p); p.y = s; G[e].push_back(p); } for(int i = 0; i < w; i++) { cin >> s >> e >> t; p.y = e, p.w = -t; G[s].push_back(p); } int ans = spfa(1); if(ans) cout << "YES" << endl; else cout << "NO" << endl; } return 0;}

SPFA 在形式上和宽度优先搜索非常类似,不同的是宽度优先搜索中一个点出了队列就不可能重新进入队列,但是SPFA中一个点可能在出队列之后再次被放入队列,也就是一个点改进过其它的点之后,过了一段时间可能本身被改进,于是再次用来改进其它的点,这样反复迭代下去。

转载于:https://www.cnblogs.com/Tinamei/p/4678591.html

你可能感兴趣的文章
iOS多线程编程Part 2/3 - NSOperation
查看>>
[转]Unity3D学习笔记(四)天空、光晕和迷雾
查看>>
Microsoft Office Excel 不能访问文件“XXXXXXXXXXXXX.xls”。 可能的原因有:
查看>>
caioj 1082 动态规划入门(非常规DP6:火车票)
查看>>
Vue表单控件绑定
查看>>
查看tomcat启动文件都干点啥---catalina.bat(转)
查看>>
POJ 3458 Colour Sequence
查看>>
FZU 1064 教授的测试
查看>>
POJ 3684 Physics Experiment
查看>>
端口(百科)
查看>>
redis安装zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h:没有那个文件或目录
查看>>
工作中用到的正则表达式
查看>>
php读取网络文件curl,fsockopen,file_get_contents,file,fopen几种方法
查看>>
C#操作XML的方法
查看>>
作业三:读《构建之法》1-5章后感
查看>>
Mybatis上路_05-使用命令行自动生成【转】
查看>>
hibernate Criteria(QBC)
查看>>
Cookie &amp;&amp; Session &amp;&amp; Token
查看>>
Java代码格式
查看>>
个人成就故事
查看>>