uva12519
| 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 (1
i
N, 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的更多相关文章
随机推荐
- 如何通过jmeter使用beanshell进行关联
关联,大多数都是通过响应的信息抓取部分信息,例如session或者hidden等 在jmeter中要使用关联,分为以下2步: Step 1. 在Sampler请求下添加正则表达式,获得信息,添加 &g ...
- Vue相关(过渡动画)
Vue 过渡 && 动画 一.CSS过渡 1.transition标签可以用来封装需要过渡的元素,添加entering/leaving 过渡, 条件是: (1)使用条件渲染语句 v-i ...
- spring配置datasource三种方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...
- python re group()
python group() 正则表达式中,group()用来提出分组截获的字符串,()用来分组 import re a = "123abc456" print re.search ...
- Unity黑巧克力 滚球游戏 入门级教程
<黑巧克力>系列教程是适合于新手上手Unity的教程,本教程适合作为初次接触Unity(零基础)的第一篇的教程.学习本教程需要有的基础是:线性代数.编程基础.Csharp语言基础.Unit ...
- 【集美大学1411_助教博客】团队作业6——展示博客(Alpha版本)
写在前面的话 工作还真是应该抓紧做呢,以下评分是助教在出差前评的,但出差回来就忘记了大部分内容.同学们都在预期时间内完成了自己的alpha项目.由于助教的频繁出差,评分工作落下一大截,在此表示欠意,会 ...
- 团队作业八—第二次团队冲刺(Beta版本) 第 1 天
一.每个人的工作 (1) 昨天已完成的工作 由于是才刚开始冲刺,所以没有昨天的工作 (2) 今天计划完成的工作: 对界面的优化和一些细节的完善 (3) 工作中遇到的困难: 工作中出现了意见不一的情况 ...
- 团队作业8——第二次项目冲刺(Beta阶段) 5.19
Day1--5.19 1.展开站立式会议(拍摄者:武健男): 会议内容:(1)新成员自我介绍,使大家能更快熟悉并一起合作. (2)由于我们之前的项目经理去了别的小组,所以我们投票选取新成员林乔桦作为我 ...
- 团队作业4——第一次项目冲刺(Alpha版本)4.23
·本次会议为第二次Scrum Meeting会议~ ·本次会议项目经理召开时间为17:00,在教学楼教室召开,召开时长约30分钟,探讨了昨日任务的进展.查重功能.今日要做的任务以及后续所要开展的工作. ...
- 201521123032 《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 1. clone方法 1.1 Object ...