UVA 558 wormholes

Source: Internet
Author: User
Tags integer numbers

Original title:
in the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through space and time connecting both star systems. Wormholes has a few peculiar properties:

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

A brilliant physicist, living on Earth, wants to use wormholes to study the Big Bang. Since Warp drive had not been invented yet, it was not possible for hers to travel from one star system to another one Direc tly. This can is done using wormholes, of course.

The scientist wants to reach a cycle of wormholes somewhere in the universe that causes she to end up in the past. By travelling along this cycle a lot of times, the scientist was able to go back as far in time as necessary to reach the B Eginning 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 is analysed. Each case is starts with a line with the numbers n and M. These indicate the number of star systems (1≤n≤1000 1 \le n \le) and the number of wormholes (0≤m≤2000 0 \le m \le 2000). The star systems is numbered from 0 (our solar system) through n-1. For each wormhole a line containing three an 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 numb Ered y, thereby ending up T (−1000≤t≤1000-1000 \le t \le) years in the future.
Output

The output consists of C lines, one line for each case, containing the word possible if it's indeed possible to go back I n time indefinitely, or not possible if the-is isn't 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
Effect:
(From Lucky Cat)
In 2163, humans discovered wormholes, a time-empty tunnel that can travel between galaxies, with a few special points:
 
The wormholes are single-direction.
The movement within the wormhole can be ignored.
There are two ends of the wormhole, which are divided into two different galaxies.
A galaxy may have multiple wormhole ends.
Based on an unknown cause, the galaxies in our planet can be found in one or more wormholes to any other galaxy.
There is a maximum of one insect hole from a galaxy to a B galaxy.
There is no hole in the same galaxy as the end.
The two ends of the wormhole will have a time difference, for example, a wormhole can allow people to traverse the future of the 15, and another wormhole can bring people back to the past 42 years ago.

One of the gifted physicists who lived on Earth, she wanted to travel back to the beginning of the universe with a wormhole, and went to the time to study the Big Bang Theory, Bang. She wanted to find out if there was a way to get her back to the endless past and bring her back to the beginning of the universe. Please write a program to determine if this cycle exists.
Input

The first column in the input has an integer C representing the number of tests.
The first column of each group has two integers, N and M, which represent the total number of galaxies n (1 <= n <= 1000) and the total number of insects (0 <= M <= 2000). The number of galaxies starts from 0 to n-1. A galaxy of 0, the solar system, was developed. And then there's an M-column that shows the information about the wormhole, with three integers x, y, T in each column, which means that the wormhole from the X-Galaxy to the Y-galaxy allows people to travel to the future of T-year ( -1000 <= T <= 1000).

Output

Output a total of column C, each column represents the results of each test, if you can find an endless loop back to the past to output "possible", if not, please output "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; }

Answer:
lets you find the negative ring, the bare bellman-ford algorithm. Directly judge the negative ring, code from the Challenge Program design contest that book, very useful ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.