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的更多相关文章
随机推荐
- 面试题收集---grep和find的区别
grep是通过文件找内容 find 是通过内容找文件 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来. 而linux下的find, 在目录结构 ...
- SQL查询语句分类
SQL查询语句有多种,下面总结下.首先先建三张表用于后面的实验 -- 学生表,记录学生信息 CREATE TABLE student( sno ), sname ), ssex ENUM('男','女 ...
- chrome开发工具指南(二)
Application 面板 使用 App Manifest 窗格检查您的网络应用清单和触发 Add to Homescreen 事件. 使用 Service Worker 窗格执行与服务工作线程相关 ...
- sqlserver与mysql中vachar(n)中遇到的坑
前两天在做将mysql的数据表导入到sqlserver当中. 本人比较愚笨,操作方法 是先将mysql的数据表到处为insert脚本,再在sqlserver中执行sql脚本 在网上看了一下那些方法 , ...
- 那些年,我们不懂的却又不得不提的 JAVA异常和异常处理!
---恢复内容开始--- 首先,我是个小小的菜鸟,最近突然突发奇想,想研究一下java的异常和异常的处理,稍有些理解,老鸟们莫要嘲笑... 既然要讲异常和异常的处理,我们就要先了解异常,那么,什么是异 ...
- GIF、JPEG 和 PNG的区别在哪…
原文地址:GIF.JPEG 和 PNG的区别在哪里?作者:苗得雨 GIF.JPEG 和 PNG 是三种最常见的图片格式. GIF:1987 年诞生,常用于网页动画,使用无损压缩,支持 256 种颜色( ...
- [转载]浏览器事件window.onload、onfocus、onblur、ons
原文地址:浏览器事件window.onload.onfocus.onblur.onscroll和resize作者:lilyxiao <html> <head> <titl ...
- 英语学习app案列分析
很多同学有误解,软件工程课是否就是理论课?或者是几个牛人拼命写代码,其他人打酱油的课?要不然就是学习一个程序语言,搞一个职业培训的课?都不对,软件工程有理论,有实践,更重要的是分析,思辨,总结.在课程 ...
- 第二次作业——个人项目实战(Sudoku)
Github:Sudoku 项目相关要求 利用程序随机构造出N个已解答的数独棋盘 . 输入 数独棋盘题目个数N 输出 随机生成N个 不重复 的 已解答完毕的 数独棋盘,并输出到sudoku.txt中, ...
- 201521123111《Java程序设计》第4周学习总结
1. 本章学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. Answer: - 上课还讲了tostring的使用,般toString用于返回表示对象值的 ...