uva 558Wormholes

来源:互联网 发布:微信小程序制作淘宝客 编辑:程序博客网 时间:2024/05/17 08:32

原题:
In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting two star systems. Wormholes have a few peculiar properties:

Wormholes are one-way only.
The time it takes to travel through a wormhole is negligible.
A wormhole has two end points, each situated in a star system.
A star system may have more than one wormhole end point within its boundaries.
For some unknown reason, starting from our solar system, it is always possible to end up in any star system by following a sequence of wormholes (maybe Earth is the centre of the universe).
Between any pair of star systems, there is at most one wormhole in either direction.
There are no wormholes with both end points in the same star system.
All wormholes have a constant time difference between their end points. For example, a specific wormhole may cause the person travelling through it to end up 15 years in the future. Another wormhole may cause the person to end up 42 years in the past.

A brilliant physicist, living on earth, wants to use wormholes to study the Big Bang. Since warp drive has not been invented yet, it is not possible for her to travel from one star system to another one directly. This can be done using wormholes, of course.

The scientist wants to reach a cycle of wormholes somewhere in the universe that causes her to end up in the past. By travelling along this cycle a lot of times, the scientist is able to go back as far in time as necessary to reach the beginning of the universe and see the Big Bang with her own eyes. Write a program to find out whether such a cycle exists.

Input

The input file starts with a line containing the number of cases c to be analysed. Each case starts with a line with two numbers n and m . These indicate the number of star systems ( 1n1000) and the number of wormholes ( 0m2000) . The star systems are numbered from 0 (our solar system) through n-1 . For each wormhole a line containing three integer numbers x, y and t is given. These numbers indicate that this wormhole allows someone to travel from the star system numbered x to the star system numbered y, thereby ending up t ( 1000t1000) years in the future.
Output

The output consists of c lines, one line for each case, containing the word possible if it is indeed possible to go back in time indefinitely, or not possible if this is not possible with the given set of star systems and wormholes.
Sample Input

2
3 3
0 1 1000
1 2 15
2 1 -42
4 4
0 1 10
1 2 20
2 3 30
3 0 -60
Sample Output

possible
not possible
大意:
(来自lucky 猫)
西元2163年,人類發現蟲洞,蟲洞是一條時空隧道,可以穿梭於星系間的時空,它有幾個特點:
 
蟲洞是單向的。
蟲洞內的移動時間可被忽略。
蟲洞有兩個端點,分別處在兩個不同的星系。
一個星系可能會有多條蟲洞的端點。
基於某種不明的原因,由我們地球所處的星系出發,一定可以藉由一或多條蟲洞到達其他任一星系。
由A星系到B星系的蟲洞最多只有一條。
不會有起點與終點皆在同一星系的蟲洞。
蟲洞的兩個端點間會有時差,例如,某條蟲洞可以讓人穿越到15年後的未來,而另一條蟲洞可以讓人回到42年前的過去。

有一位住在地球上的天才物理學家,她想藉由蟲洞旅行回到宇宙創始的時候,親自到那個時空研究大爆炸理論(Big Bang)。她想找出是否有一條循環路徑可以讓她回到無止盡的過去,帶她回到宇宙創始之際。請你寫一個程式判斷這樣的循環是否存在。 
Input

輸入的第一列有一個整數 c 表示測試資料的組數。
每組測試資料的第一列有兩個整數 n 與 m,分別表示星系總數n(1 <= n <= 1000)與蟲洞總數m(0 <= m <= 2000)。星系的編號由 0 開始到 n-1 。以編號 0 的星系,也就是太陽系,為出發點。接下來有 m 列表示蟲洞的資料,每列有三個整數 x, y, t,分別表示由 x 星系到 y 星系的蟲洞能讓人旅行到 t 年後的未來(-1000 <= t <= 1000)。

Output

輸出共有 c 列,每列表示每組測試資料的結果,若可以找到無止盡回到過去的循環請輸出”possible”,若不行請輸出”not possible”。

#include <bits/stdc++.h>using namespace std;const int MAX_E=2005;const int MAX_V=1005;struct edge{    int from,to,cost;};edge es[MAX_E];int d[MAX_V];int V,E;bool find_negative_loop(){    memset(d,0,sizeof(d));    for(int i=0;i<V;i++)    {        for(int j=0;j<E;j++)        {            edge e=es[j];            if(d[e.to]>d[e.from]+e.cost)            {                d[e.to]=d[e.from]+e.cost;                if(i==V-1)                    return true;            }        }    }    return false;}int main(){    ios::sync_with_stdio(false);    int t;    cin>>t;    while(t--)    {        cin>>V>>E;        for(int i=0;i<E;i++)            cin>>es[i].from>>es[i].to>>es[i].cost;        if(find_negative_loop())            cout<<"possible"<<endl;        else            cout<<"not possible"<<endl;    }    return 0;}

解答:
让你找负的环,裸的bellman-ford算法。直接判断负环即可,代码来自挑战程序设计竞赛那本书,很好用~

0 0
原创粉丝点击