点击打开链接

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25582   Accepted: 9186

Description

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms
comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..NM (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000
seconds.

Input

Line 1: A single integer, FF farm descriptions follow. 

Line 1 of each farm: Three space-separated integers respectively: NM, and W 

Lines 2..M+1 of each farm: Three space-separated numbers (SET) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected
by more than one path. 

Lines M+2..M+W+1 of each farm: Three space-separated numbers (SET) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

Sample Input

  1. 2
  2. 3 3 1
  3. 1 2 2
  4. 1 3 4
  5. 2 3 1
  6. 3 1 3
  7. 3 2 1
  8. 1 2 3
  9. 2 3 4
  10. 3 1 8

Sample Output

  1. NO
  2. YES

Hint

For farm 1, FJ cannot travel back in time. 

For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.

题目大意是给你一些虫洞和路径,通过虫洞可以回到过去,题目问你是否经过这些虫洞可以回到最初开始的地方,并且时间还要是before

bellman-ford或者SPFA都能做,就是求负回路,bellman-ford的判断方法就是全图松弛 n - 1 次,如果还能继续松弛,说明有负圈回路,否则就说明没有负圈回路,bellman-ford有一个优化就是松弛的过程中如果在一次松弛过程中任何点都没有改变最短路径,那么就可以提前结束全图的松弛操作得到结果

spaf是bellman算法的一个优化,每次松弛的不是全图的点,而是与最短距离有变化的点相连的那些点,题目用到一个队列,每个点如果最短路径更新了,就扔到队列里,然后从队列中拿出点来松弛与其相连的点,如果一个点入队列的次数达到n此,说明图中有负圈

注意两个点之间可能有多条路径,选择权值较小的那条边留下

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<utility>
  4. #include<queue>
  5. using namespace std;
  6.  
  7. int map[501][501];
  8. int dis[501];
  9. int n, m, w;
  10. int s, e, t;
  11.  
  12. bool spfa()
  13. {
  14. bool flag[501] = {0};
  15. int count[501] = {0};
  16. queue<int > q;
  17. q.push(s);
  18. dis[s] = 0;
  19. int curr;
  20. int i;
  21. while(!q.empty())
  22. {
  23. curr = q.front();
  24. q.pop();
  25. for(i = 1; i <= n; i++)
  26. {
  27. if(map[curr][i] < 100000)
  28. {
  29. if(dis[i] > map[curr][i] + dis[curr] )
  30. {
  31. dis[i] = map[curr][i] + dis[curr];
  32. if(flag[i] == 0)
  33. q.push(i);
  34. count[i] ++ ;
  35. flag[i] = 1;
  36. if(count[i] >= n)
  37. return 0;
  38. }
  39. }
  40. }
  41. flag[curr] = 0;
  42. }
  43. return 1;
  44. }
  45. int main()
  46. {
  47. int f;
  48. scanf("%d", &f);
  49. while(f--)
  50. {
  51.  
  52. memset(dis,63, sizeof(dis));
  53. memset(map, 127, sizeof(map));
  54. scanf("%d %d %d", &n, &m, &w);
  55.  
  56. int i;
  57. for(i = 0; i < m; i++)
  58. {
  59. scanf("%d %d %d", &s, &e, &t);
  60. map[s][e] = map[s][e] > t? t : map[s][e];
  61. map[e][s] = map[e][s] > t? t : map[e][s];
  62. }
  63. for(i = 0; i < w; i++)
  64. {
  65. scanf("%d %d %d", &s, &e, &t);
  66. map[s][e] = -t;
  67. }
  68. if(spfa())
  69. printf("NO\n");
  70. else
  71. printf("YES\n");
  72. }
  73. return 0;
  74. }

poj 3259 Wormholes spfa算法的更多相关文章

  1. POJ 3259 Wormholes SPFA算法题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  2. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  3. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  4. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  5. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  6. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  7. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  8. POJ 3259 Wormholes (Bellman_ford算法)

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

  9. POJ 3259 Wormholes(SPFA)

    http://poj.org/problem?id=3259 题意 : 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地, ...

随机推荐

  1. JS刷新页面的几种方法(转)

    Javascript刷新页面的几种方法: 1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locat ...

  2. Android Studio如何添加override

    而Android Studio如何添加呢?方法如下: 右键(或者Alt + Insert) ---  Generate... ---- Override Method...  或者 Implement ...

  3. 数据库表转换成javaBean对象小工具

    package test.utils; import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter; ...

  4. RSA加密工具包

    主要参考: http://www.blogjava.net/icewee/archive/2012/05/19/378570.html http://snowolf.iteye.com/ 基于以上代码 ...

  5. IOS-归档的使用

    归档又叫序列化(coding) //   归档NSKeyedArchiver //   解归档NSKeyedUnarchiver // 归档之后的文件 会被加密 //  可以归档的文件.数据--> ...

  6. ios外派—本公司长年提供ios程序员外派业务(北京动点软件,可签合同)

    北京动点飞扬长年提供ios工程师外派业务. 我公司程序员平均技术情况如下: 1.二年以上iPhone/ipad开发经验:2.熟练使用Xcode.Objective C编码技能:3.熟悉iOS开发框架, ...

  7. asp.net 页面 输出之前修改 html(render)

    protected override void Render(HtmlTextWriter writer) { StringWriter output = new StringWriter(); ba ...

  8. "XX cannot be resolved to a type "eclipse报错及解决说明

    转自:http://zhaoningbo.iteye.com/blog/1137215 引言: eclipse新导入的项目经常可以看到“XX cannot be resolved to a type” ...

  9. Android:单元测试Junit的配置

    在实际开发中,开发android软件的过程需要不断地进行测试.而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性.... ...

  10. [复变函数]第11堂课 3.3 Cauchy 积分定理及其推论

    0. 引言 (1) Cauchy 积分定理: 设 $D$ 为 $(n+1)$ 连通区域, $f$ 在 $D$ 内解析且连续到边界 $C$, 则 $\dps{\int_C f(\zeta)\rd \ze ...