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. Bootstrap-table使用记录(转)

    HTML代码 /*index.cshtml*/ @section styles{ <style> .main { margin-top:20px; } .modal-body .form- ...

  2. JavaScript实现省市联动

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  3. ireport导出中英文pdf

    准备: 报表开发工具:ireport 5.6.0 报表框架: jasperreport5.6.0 1.中文格式设置pdf fontname.isembedded.pdfencoding pdf fon ...

  4. Netty自娱自乐之协议栈设计

    ---恢复内容开始--- 俺工作已经一年又6个月了,想想过的真快,每天写业务,写业务,写业务.......然后就是祈祷着,这次上线不要出现线上bug.继续这每天无聊的增删改查,学习学习一下自己感兴趣的 ...

  5. ant安装以及环境变量配置、验证

    (一)安装 ant 下载地址: http://ant.apache.org/     根据自己电脑下载对应版本 下载完成以后,可自行解压到自己常用的盘中,但是要记住解压到哪里了,以便后续的环境变量配置 ...

  6. 事后诸葛亮分析(Beta版本)

    全组讨论的照片 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决代码分析.统计.管理等问题,定义的很清楚,有清晰的描述. 是否有充足的时间来做计划? ...

  7. 团队作业4--第一次项目冲刺(Alpha版本) 4

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 完成对查重结果的写出与保存,将查重结果写出并导出保存为Excel形式 四.困难与问题 对查重结果的保存,当有多份文档进行比较的 ...

  8. 团队作业7---Alpha冲刺值事后诸葛

    一.设想和目标 1.我们的软件要解决什么问题? 解决教师和助教对实验报告查重的问题,拥有两个用户:1.教师或助教:查看学生实验报告的重复率:4.学生:上传实验报告. 2.是否定义得很清楚?是否对典型用 ...

  9. 个人作业(3)----个人总结(Alpha阶段)

    一.个人总结. 个人完成的任务:在此阶段我完成了用户调研.部分测试以及部分博客书写. 个人及团队心得:经过几周Alpha阶段开发后,我大致了解了开发软件的过程,开发一个软件并没有以往想象中那么简易,在 ...

  10. java201521123118《java程序设计》第3周总结

    1. 本周学习 总结初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 2. 书面作 ...