The Farnsworth Parabox

Professor Farnsworth, a renowned scientist that lives in year 3000 working at Planet Express Inc., performed a failed experiment that nearly killed him. As a sub-product, some strange boxes were created. Farnsworth gave one of the boxes to Leela, who accidentally discovered that it leads to a parallel universe. After that, the Planet Express crew traveled to the new discovered parallel universe using the box, meeting their corresponding parallel copies, including a parallel Professor Farnsworth who also created some boxes.

Simultaneously, some parallel copies of the Professor created similar boxes in some existing parallel universes. As a result, some universes, including the original one, were endowed with a (possibly empty) collection of boxes leading to other parallel universes. However, the boxes have a bug: besides allowing travels among different parallel universes, they allow for time travels. So, a particular box leads to a distinct parallel universe possibly allowing a voyager to gain or lose a certain number of time units.

One of the boxes invented by Farnsworth Professor, from Futurama. ©The Curiosity Company and 20thCentury Fox.

More precisely, given two distinct universes A and B, and a non-negative integer number t, a (A, B)-box with time displacement t is an object designed to travel between the two universes that can be used directly (traveling from A to B) or reversely (traveling from B to A). A such box exists in both universes, allowing travels among both universes. A voyager that uses the (A, B)-box directly can travel from universe A to universe B landing t time units in the future. On the other hand, a voyager that uses the (A, B)-box reversely can travel from universe B to universe A landing t time units in the past. Box building requires so much energy that there may be built at most one box to travel between a given pair of different universes.

A circuit is defined as a non-empty sequence of parallel universes $
-->s1, s2,..., sm such that:

  • The first and the last universe in the sequence are the same (i.e., s1 = sm).
  • For every k ( 1 k < m) there is a (sk, sk+1)-box or a (sk+1, sk)-box to travel (directly or reversely) from universe sk to universe sk+1.

The possible existence of circuits is very interesting. Using the corresponding boxes of a circuit, a voyager may experiment real time travels. Professor Farnsworth wants to know if there is a circuit that starts in the original universe and allows travels to the past, constituting a phenomenon known as the Farnsworth Parabox. For example, imagine that there are three universes, A, B and C, and that there exist the following boxes: a (A, B)-box with time displacement 3, a (A, C)-box with time displacement 2, and a (B, C)-box with time displacement 4. Clearly, the sequence $
-->A, B, C, A is a circuit, that allows to travel five time units in the future, starting and ending at universe A.

The original Farnsworth Professor, who lives in the original universe, wants to know if the Farnsworth Parabox is true or not. Can you help him?

Input

There are several cases to solve. Each case begins with a line with two integer numbers N and B, indicating the number of parallel universes (including the original) and the number of existing boxes, respectively ( 2 N 102, 1 B N . (N - 1)/2). The distinct universes are identified uniquely with the numbers 1, 2,..., N, where the original universe is the number 1. Each one of the next B lines contains three integer numbers i, j and t, describing a (i, j)-box to travel between the universe i and the universe j with time displacement t (1iN, 1 j N, i j, 0 t 102). The input ends with a line with two 0 values.

Output

For each test case output one line with the letter `Y' if the Farnsworth Parabox is true; or with the letter `N', otherwise.

Sample Input

2 1
2 1 1
3 3
1 2 3
1 3 2
2 3 4
4 4
1 2 2
3 2 2
3 4 2
1 4 2
0 0

Sample Output

N
Y
N

找不为0的环:
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <math.h>
#include <set>
#include <vector>
using namespace std;
int n,m;
vector<pair<int,int> >a[];
int b[];
int c[];
int dfs(int x,int z)
{
if(b[x])
{
if(c[x]!=z)
return ;
else return ;
}
c[x]=z;
b[x]=;
int i;
int size=a[x].size();
for(i=;i<size;i++)
{
if(dfs(a[x][i].first,z+a[x][i].second))return ;
}
return ;
}
int main()
{
//freopen("int.txt","r",stdin);
int i;
while(scanf("%d%d",&n,&m))
{
if(n==&&m==)break;
memset(b,,sizeof(b));
memset(c,,sizeof(c));
for(i=;i<=n;i++)
a[i].clear();
int x,y,z;
for(i=;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
a[x].push_back(make_pair(y,z));
a[y].push_back(make_pair(x,-z));
}
if(dfs(,))
cout<<"Y"<<endl;
else cout<<"N"<<endl;
}
}

uva12519的更多相关文章

随机推荐

  1. Ext.Ajax.request

    function create(){ var itstate = $("#myselect").val(); Ext.Ajax.request({ url: '/servlet/A ...

  2. 哪有Python视频百度云链接?

    说到Python视频哪个好,小编在这里不好下结论.毕竟孰好孰坏,只有合适的才是最好的.就像买鞋子,一双鞋子好不好,只有合脚才是真的好.不过,这里有些个人认为通俗易懂的Python视频可以分享给大家. ...

  3. JQuery操作iframe父页面与子页面的元素与方法

    JQuery操作iframe父页面与子页面的元素与方法 JQUERY IFRAME 下面简单使用Jquery来操作iframe的一些记录,这个使用纯JS也可以实现. 第一.在iframe中查找父页面元 ...

  4. vue系列之动态路由【原创】

    开题 最近用vue来构建了一个小项目,由于项目是以iframe的形式嵌套在别的项目中的,所以对于登录的验证就比较的麻烦,索性后端大佬们基于现在的问题提出了解决的方案,在看到他们的解决方案之前,我先画了 ...

  5. Linux io Model

    socket阻塞与非阻塞,同步与异步 作者:huangguisu 1. 概念理解 在进行网络编程时,我们常常见到同步(Sync)/异步(Async),阻塞(Block)/非阻塞(Unblock)四种调 ...

  6. PHP常量定义define与const

    一.const PHP5.3以前,const只能在类内部声明变量,5.3+允许在外部声明变量,但还不能使用常量计算! const ONE = 1; const WORD = 'hello world' ...

  7. eval浅解

    关于eval,你了解多少呢?来看看 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 需要一个参数(string),切必需.要计算的字符串,其中含有要计算的 JavaS ...

  8. java程序的内存分配

    java程序的内存分配 JAVA 文件编译执行与虚拟机(JVM)介绍 Java 虚拟机(JVM)是可运行Java代码的假想计算机.只要根据JVM规格描述将解释器移植到特定的计算机上,就能保证经过编译的 ...

  9. sudoku--设想

    在查阅了一些资料和自己动手写一写后,找到一种可行的解法. 第一步 首先将9x9的数独方格分成九份3x3的九宫格,如下图 B1 B2 B3 B4 B5 B6 B7 B8 B9 而后在左上角的B1上随机生 ...

  10. 201521123012 《Java程序设计》第七周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代 ...