J - Wormholes

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Description

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 ( )
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 xy 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.

cid=84319" style="color:blue; text-decoration:none">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.

cid=84319" style="color:blue; text-decoration:none">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

Miguel A. Revilla

1998-03-10

题意:问能否通过虫洞,回到过去,意思实际上就是求,最短路里面有不有负环。

思路:SPFA 或者Bellman-ford 推断下有不有负环就能够了,对于SPFA,假设有负环,表明进栈次数大于等于n次。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define INF 9999999
using namespace std; int n,m;
int num[2222];
int dis[2222];
int vis[2222];
int f[2222];
int u[2222],v[2222],w[2222],next[2222]; int spfa()
{
queue<int>q;
memset(vis,0,sizeof vis);
memset(num,0,sizeof num); for(int i=0;i<=n;i++) dis[i]=INF;
dis[0]=0;
q.push(0);
num[0]++; while(!q.empty())
{
int x=q.front(); q.pop();
vis[x]=0;
for(int i=f[x];i!=-1;i=next[i])
if(dis[x]+w[i]<dis[v[i]])
{
dis[v[i]]=dis[x]+w[i];
if(vis[v[i]]==0)
{
vis[v[i]]=1;
q.push(v[i]);
num[v[i]]++;
if(num[v[i]]>=n)
return 1;
}
}
} return 0;
} int main()
{
int T;
scanf("%d",&T); while(T--)
{
scanf("%d%d",&n,&m);
memset(f,-1,sizeof f); for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u[i],&v[i],&w[i]);
next[i]=f[u[i]];f[u[i]]=i;
} if(spfa()==0)
puts("not possible");
else
puts("possible"); }
return 0;
} /*
5 4
1 2 1
1 3 2
2 4 3
2 5 2
*/

uva558 Wormholes SPFA 求是否存在负环的更多相关文章

  1. POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 49962   Accepted: 18421 Descr ...

  2. POJ3259 Wormholes —— spfa求负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  3. vijos1053 用spfa判断是否存在负环

    MARK 用spfa判断是否存在负环 判断是否存在负环的方法有很多, 其中用spfa判断的方法是:如果存在一个点入栈两次,那么就存在负环. 细节想想确实是这样,按理来说是不存在入栈两次的如果边权值为正 ...

  4. poj 3259 Wormholes 【SPFA&amp;&amp;推断负环】

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36852   Accepted: 13502 Descr ...

  5. spfa算法及判负环详解

    spfa     (Shortest Path Faster Algorithm) 是一种单源最短路径的算法,基于Bellman-Ford算法上由队列优化实现. 什么是Bellman_Ford,百度内 ...

  6. SPFA算法的判负环问题(BFS与DFS实现)

    经过笔者的多次实践(失败),在此温馨提示:用SPFA判负环时一定要特别小心! 首先SPFA有BFS和DFS两种实现方式,两者的判负环方式也是不同的.       BFS是用一个num数组,num[x] ...

  7. poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】

    题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...

  8. (简单) POJ 3259 Wormholes,SPFA判断负环。

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  9. POJ3259:Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 68097   Accepted: 25374 题目链接: ...

随机推荐

  1. 《少年先疯队》第九次团队作业:Beta冲刺第三天

    3.1 今日完成任务情况 姚玉婷:酒店系统中剩余功能的完善 马丽莎:酒店系统中管理员功能的测试 张   琼:酒店系统中会员功能的测试 孙苗坤:酒店系统中其余管理功能的测试文档的编写 3.2 成员贡献时 ...

  2. 《发际线总是和我作队》第八次团队作业:Alpha冲刺 第五天

    项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺实验十一 团队作业7:团队项目设计完善&编码 团队名称 发际线总和我作队 作业学 ...

  3. List<T>排序

    List<Student> studentList = new List<Student>(); Student s = new Student(); s.Name = &qu ...

  4. linux 05

    日期与时间命令:date.日历的命令:cal.计算器:bc  要使用quit退出 在命令行模式里执行命令时,会有两种主要情况: 一种是该命令会直接显示结果然后回到命令提示符等待下一个命令的输入 dat ...

  5. Python:字体颜色

    /033[31;1m     /033[0m 字体背景颜色 40:黑 41:深红 42:绿 43:黄色 44:蓝色 45:紫色 46:深绿 47:白色 字体颜色 30:黑 31:红 32:绿 33:黄 ...

  6. 「问题思考」python的递归中return返回none

    代码: #求最大公约数 def gcd(x,y): if x < y: swap = x x = y y = swap if x%y == 0: return y else: gcd(y,x%y ...

  7. UVALive - 6267 Beer Pressure

    题意: 给定n个酒吧, 然后有k个学生投票今晚去哪个酒吧, 然后会有a个(a<=k)学生先投票了, 先投的票会影响后面的人投票的概率, 求每个酒吧今晚去的概率. 分析: 我们可以从最初的状态开始 ...

  8. 【06】Firebug记录Javascript日志

    Firebug记录Javascript日志 你可以使用Firebug来生成日志. 这有助于我们调试web页面并发现页面的错误. 在Firefox浏览器中执行以下代码: <!DOCTYPE HTM ...

  9. 记第一次开发安卓应用——IT之家RSS阅读器

    这个学期学校开了安卓的课程,因为自己一直学习wp的开发,一直用的是.net和Silverlight这一套,也着实没有太多时间投入安卓的方向去,因为想着毕业也不从事安卓的工作,所以也一直没有怎么研究.但 ...

  10. 大数据学习——hive基本操作

    1 建表 create table student(id int,name string ,age int) row format delimitedfields terminated by ','; ...