- 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 ( ) and the number of wormholes ( ) . 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 ( ) 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
possiblenot possible
题意: n个恒星,m条空间虫洞,边有正负,代表穿越未来和过去,先要最后回到过去,就是问是否有一个负环 。
最短路中权有负的时候用SPFA, 当起点到该点间有一个环时,那么这个点就会不停的更新值,因为正环可以无限大,负环可以无限小,(转任意圈) ;
所以只要判断某个点是否是否更新了n次即可 ;
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<stack>
#include<queue>
#include <iomanip>
#include<iostream>
#include<algorithm>
using namespace std ;
const int N=1010 ;
const int M=2100 ;
const int inf=1<<30 ;
struct node
{
int u,v,c,next;
}edge[M];
int vist[N],head[N],coust[N],dist[N];
int top,n,m;
void add(int u,int v,int c)
{
edge[top].u=u;
edge[top].v=v;
edge[top].c=c;
edge[top].next=head[u];
head[u]=top++;
}
int SPFA()
{
memset(vist,1,sizeof(vist));
memset(coust,0,sizeof(coust));
memset(dist,0,sizeof(dist));
int u,v;
queue<int>q;
// vist[0]=1;
for(int i = 0 ; i < n ; i++)
q.push(i);
while(!q.empty())
{
u=q.front();
q.pop();
vist[u]=0;
for(int i =head[u]; i!=-1 ; i=edge[i].next)
{
v=edge[i].v;
if(dist[v] > dist[u]+edge[i].c)
{
coust[v]++;
dist[v] = dist[u]+edge[i].c ;
if(coust[v]==n) return 1;
if(!vist[v])
{
vist[v]=1;
q.push(v);
}
}
}
}
return 0;
}
int main()
{
int t ,u,v,c;
scanf("%d",&t);
while(t--)
{
top=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&u,&v,&c);
add(u,v,c);
}
int ans=SPFA();
if(ans) printf("possible\n");
else printf("not possible\n");
}
return 0;
}