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的更多相关文章
随机推荐
- pip 警告!The default format will switch to columns in the future
pip警告! DEPRECATION: The default format will switch to columns in the future. You can use --format=(l ...
- Entity Framework Core 2.0 数据库迁移
看见过几篇其他大神写的关于EFCore2.0的文章.本人有点小白,一开始看文档的时候除了一些基本操作外其他部分几乎没有读懂,我估计会有一部分人跟我一样,因为人家读懂了的早就懂了. 在这里我写一下我自己 ...
- 九度OJ 1006 ZOJ
#include <iostream> #include <string> using namespace std; int getO(string str,int & ...
- 四则运算GUI
一.题目描述 我们在个人作业1中,用各种语言实现了一个命令行的四则运算小程序.进一步,本次要求把这个程序做成GUI(可以是Windows PC 上的,也可以是Mac.Linux,web,手机上的),成 ...
- 集美大学网络1413第十一次作业成绩(团队七) -- Alpha冲刺之事后诸葛亮
题目 团队作业7--Alpha冲刺之事后诸葛亮 团队作业7成绩 团队/分值 设想和目标 计划 资源 变更管理 设计/实现 测试/发布 团队角色. 管理.合作 总结 讨论照片 团队成员 角色.贡献 总 ...
- 团队作业4——第一次项目冲刺 tHe LaSt dAy
项目冲刺--终点 敏捷冲刺最后一天,没想到前一天就上榜了,我也很无奈啊. 那今天就老老实实写一篇博客好了. Mission 这次敏捷冲刺呢,我们主要做了前端页面,后台的数据库和添加了基本的增删查改的功 ...
- 201521123121 《JAVA程序设计》第7周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 ...
- 201521044091 java 第一周总结
1.本周学习总结 (1)第一次开始接触java语言,有些用法还是和c和c++有点差异,需要不断去学习 (2)java其实不仅仅一种高级语言,它包括的还有它的整套体系. 2. 书面作业 1.为什么jav ...
- JAVA课程设计 刘舒婷 201521123096
1.团队课程设计博客链接 2.个人负责模块说明 2.1 界面菜单的设计: 2.2 录入学生信息: 2.3 对已录入的学生信息进行修改: 3.个人代码提交记录截图 4.自己负责模块或任务详细说明 4.1 ...
- Linux SDK之uClinux、Broadcom、Atheros、Realtek、Ralink、Marvell、Intel
接触的Linux SDK越来越多,整理整理,分享分享,不求系统全面,对您有帮助便足矣 文中大部分是与AP/Router SoC解决方案(单芯片WIFI 路由器解决方案)相关的Linux SDK SDK ...