• 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.

    epsfbox{p12519.eps} 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 $ left<vphantom{s_1,s_2,ldots,s_m}
ight.$s1, s2,..., sm$ left.vphantom{s_1,s_2,ldots,s_m}
ight>$ such that:

    • The first and the last universe in the sequence are the same (i.e., s1 = sm).
    • For every k ( 1 $ leq$ 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 $ left<vphantom{A,B,C,A}
ight.$A, B, C, A$ left.vphantom{A,B,C,A}
ight>$ 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 $ leq$ N $ leq$ 102, 1 $ leq$ B $ leq$ 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$ leq$i$ leq$N, 1 $ leq$ j $ leq$ N, i $ 
eq$ j, 0 $ leq$ t $ leq$ 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的环:
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <queue>
     5 #include <math.h>
     6 #include <set>
     7 #include <vector>
     8 using namespace std;
     9 int n,m;
    10 vector<pair<int,int> >a[150];
    11 int b[150];
    12 int c[150];
    13 int dfs(int x,int z)
    14 {
    15     if(b[x])
    16     {
    17         if(c[x]!=z)
    18         return 1;
    19         else return 0;
    20     }
    21     c[x]=z;
    22     b[x]=1;
    23     int i;
    24     int size=a[x].size();
    25     for(i=0;i<size;i++)
    26     {
    27         if(dfs(a[x][i].first,z+a[x][i].second))return 1;
    28     }
    29     return 0;
    30 }
    31 int main()
    32 {
    33     //freopen("int.txt","r",stdin);
    34     int i;
    35     while(scanf("%d%d",&n,&m))
    36     {
    37         if(n==0&&m==0)break;
    38         memset(b,0,sizeof(b));
    39         memset(c,0,sizeof(c));
    40         for(i=1;i<=n;i++)
    41         a[i].clear();
    42         int x,y,z;
    43         for(i=0;i<m;i++)
    44         {
    45             scanf("%d%d%d",&x,&y,&z);
    46             a[x].push_back(make_pair(y,z));
    47             a[y].push_back(make_pair(x,-z));
    48         }
    49         if(dfs(1,0))
    50         cout<<"Y"<<endl;
    51         else cout<<"N"<<endl;
    52     }
    53 }
    View Code
  • 相关阅读:
    JavaScript系列---【分析局部作用域下的预解析】
    javaScript系列---【分析全局作用域下的预解析】
    javaScript系列---【分析函数的arguments】
    JavaScript系列---【条件if--切换图片案例2 高亮及按钮同步显示】
    javaScript系列---【this详解及call和apply修改this指向】
    JavaScript系列---【选项卡案例】
    JavaScript系列---【QQ列表展开及闭合案例】
    系统安装01-CentOS6系统安装
    hdoj--3635--Dragon Balls(并查集记录深度)
    hdoj--1281--棋盘游戏(最小点覆盖+枚举)
  • 原文地址:https://www.cnblogs.com/ERKE/p/3265050.html
Copyright © 2020-2023  润新知