Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category= &problem=499&mosmsg=submission+received+with+id+10614453
Topic:
Wormholes
In the year 2163, wormholes were discovered. A wormhole is a subspace tunnel through spaces 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 the than one wormhole end point within its boundaries.
For some unknown reason, starting from our solar system, it are 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 are at most one wormhole either.
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 could cause the person travelling through it to the years. Another wormhole may cause of the person to the end of the 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, it isn't yet for possible to her one-star system to travel one another tly. This can is done using wormholes, of course.
The scientist wants to reach a cycle of wormholes somewhere, the universe that causes, to the "her". By travelling along this cycle a lot the "times", the scientist is able to go back as far into as necessary to reach the B Eginning of the universe and the 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 the a line with two numbers n and M. These indicate the number of star systems ($ le n le 1000$) and the number of wormholes ($ le m le 2000$). 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 are given. These numbers indicate, this, wormhole allows someone to travel from the star system numbered X. to the star system numb Ered y, thereby ending up t ($-1000 le t le 1000$) 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 isn't possible if this 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
Analysis and Summary:
In fact, to find a negative loop, for the SPFA algorithm, if a node updated n times, then there is a negative ring;
Using an array to record the number of updates to each point, once there is greater than or equal to N, then there is a negative loop.
Code:
#include <cstdio> #include <cstring> #include <queue> using namespace std;
const int N = 5005;
const int INF = 1000000000;
int n, M, Head[n],next[n],u[n],v[n],w[n],d[n], cnt[n];
BOOL Vis[n];
inline void Read_graph () {scanf ("%d%d", &n,&m);
Memset (Head,-1, sizeof (head));
for (int e=1; e<=m; ++e) {scanf ("%d%d%d", &u[e],&v[e],&w[e]);
Next[e] = head[u[e]];
Head[u[e]] = e;
} inline bool Spfa (int src) {memset (Vis, 0, sizeof (VIS));
memset (CNT, 0, sizeof (CNT));
for (int i=0; i<n; ++i) d[i] = INF;
D[SRC] = 0;
++CNT[SRC];
queue<int>q;
Q.push (SRC); while (!q.empty ()) {int U=q.front ();
Q.pop ();
Vis[u] = false;
for (int e=head[u]; e!=-1 e=next[e]) if (D[v[e]]>d[u]+w[e]) {d[v[e]] = d[u]+w[e];
if (!vis[v[e]]) {Q.push (v[e]); Vis[v[e]] = true;
if (++cnt[v[e]]>=n) return true;
}} return false;
int main () {int T;
scanf ("%d", &t);
while (t--) {read_graph ();
if (SPFA (0)) printf ("possible\n");
else printf ("Not possible\n");
return 0; }