链接:



Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25079   Accepted: 8946

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
comprisesN (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 Ethat 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 Ethat 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.

Source

题意:


农夫 FJ 有 N 块田地【编号 1...n】 (1<=N<=500)

        田地间有 M 条路径 【双向】(1<= M <= 2500)

        同时有 W 个孔洞,可以回到以前的一个时间点【单向】(1<= W <=200)

        问:FJ 是否能在田地中遇到以前的自己

算法:bellman_ford 判断是否有负环

思路:


田地间的双向路径加边,权值为

        孔洞间的单向路径加边,权值为【可以回到以前】

        判断有向图是否存在负环

        因为如果存在了负数环,时间就会不停的减少,

        那么 FJ 就可以回到以前更远的地方,肯定能遇到以前的自己的

PS:第一次做这个的童鞋,如果实在无法理解,就按照上面的样例和思路画个图就好了,反正才三个点。
         两年了,居然如此经典的入门题目都没有遇到过,真不知道我干什么去了Orz



code:

3259 Accepted 180K 63MS C++ 1707B

  1. /********************************************************************
  2. Accepted 180 KB 47 ms C++ 2509 B
  3. 题意:农夫 FJ 有 N 块田地【编号 1...n】 (1<=N<=500)
  4. 田地间有 M 条路径 【双向】(1<= M <= 2500)
  5. 同时有 W 个孔洞,可以回到以前的一个时间点【单向】(1<= W <=200)
  6. 问:FJ 是否能在田地中遇到以前的自己
  7. 算法:bellman_ford 判断是否有负环
  8. 思路:田地间的双向路径加边,权值为正
  9. 孔洞间的单向路径加边,权值为负【可以回到以前】
  10. 判断有向图是否存在负环
  11. 因为如果存在了负数环,时间就会不停的减少,
  12. 那么 FJ 就可以回到以前更远的地方,肯定能遇到以前的自己的
  13. *******************************************************************/
  14. #include<stdio.h>
  15. #include<string.h>
  16. #include<algorithm>
  17. #include<iostream>
  18. using namespace std;
  19.  
  20. const int maxn = 510;
  21. const int maxw = 2500*2+200+10;
  22. const int INF = 10000;
  23. int d[maxn];
  24. int n,m;
  25.  
  26. struct Edge{
  27. int u,v;
  28. int t;
  29. }edge[maxw];
  30.  
  31. bool bellman_ford()
  32. {
  33. for(int i = 1; i <= n; i++) d[i] = INF; //初始化从起点到 i 时间为最值
  34. d[1] = 0; //起点为 0
  35.  
  36. for(int i = 1; i < n; i++)
  37. {
  38. bool flag = true; //判断这轮是否能够松弛
  39. for(int j = 0; j < m; j++)
  40. {
  41. int u = edge[j].u;
  42. int v = edge[j].v;
  43. int t = edge[j].t;
  44.  
  45. if(d[v] > d[u]+t) //松弛操作
  46. {
  47. d[v] = d[u]+t;
  48. flag = false;
  49. }
  50. }
  51. if(flag) return false; //如果当前轮不能松弛,直接判断没有负数环
  52. }
  53.  
  54. for(int i = 0; i < m; i++)
  55. {
  56. if(d[edge[i].v] > d[edge[i].u]+edge[i].t)
  57. return true;//如果仍然能够松弛则存在负环
  58. }
  59. return false;
  60. }
  61.  
  62. int main()
  63. {
  64. int T;
  65. int M,W;
  66. scanf("%d", &T);
  67. while(T--)
  68. {
  69. scanf("%d%d%d", &n,&M,&W);
  70. m = 0;
  71.  
  72. int u,v,t;
  73. for(int i = 1; i <= M; i++) //田地间的大路,加双边
  74. {
  75. scanf("%d%d%d", &u,&v,&t);
  76. edge[m].u = u;
  77. edge[m].v = v;
  78. edge[m++].t = t;
  79.  
  80. edge[m].u = v;
  81. edge[m].v = u;
  82. edge[m++].t = t;
  83. }
  84.  
  85. for(int i = 1; i <= W; i++) //孔洞,加单边
  86. {
  87. scanf("%d%d%d", &u,&v,&t);
  88. edge[m].u = u;
  89. edge[m].v = v;
  90. edge[m++].t = -t;
  91. }
  92.  
  93. if(bellman_ford()) printf("YES\n"); //存在负数环
  94. else printf("NO\n");
  95. }
  96. }
















POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】的更多相关文章

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

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

  2. POJ 3259 Wormholes(SPFA判负环)

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

  3. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  4. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  5. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  6. POJ 3259 Wormholes【Bellman_ford判断负环】

    题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说 ...

  7. POJ 3259:Wormholes bellman_ford判定负环

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37906   Accepted: 13954 Descr ...

  8. poj 3259 (Bellman_Ford判断负环)

    题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...

  9. Wormholes POJ 3259(SPFA判负环)

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

随机推荐

  1. python基础语法(四)

    --------------------------------------------接 Python 基础语法(三)---------------------------------------- ...

  2. 如何将微信小程序页面内容充满整个屏幕

    修改该页面的wxss文件 /* pages/weather/weather.wxss */ .weather{ position: fixed; height: 100%; width: 100%; ...

  3. JavaScript完整性检查

    1.7个“坑” <!DOCTYPE html> <html lang="zh"> <head> <meta charset="U ...

  4. 使用CAsyncSocket总结

    最近想起CAsyncSocket这个类,记得很早以前用过,现在却想不起来怎么用了,翻了翻以前的代码又看了看msdn感觉这个类做简单的异步socket太简单了,几行代码就可以搞定,在此先做个总结. 不管 ...

  5. JavaScript在IE浏览器和Firefox浏览器中的差异总结

    JavaScript在IE浏览器和Firefox浏览器中存在一些差异,以下对这些差异部分进行了总结,以及解决方案: 1.HTML对象的 id 作为对象名的问题 IE:HTML 对象的 ID 可以作为 ...

  6. MySQL Memory 存储引擎浅析

    原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 前言 需求源自项目中的MemCache需求,開始想用MemCached(官方网站:http://memcac ...

  7. 使用caffe 的 python接口测试数据,选定GPU编号

    只需要在python脚本中添加两行代码: caffe.set_device(0) #使用第一块显卡 caffe.set_mode_gpu() #设为gpu模式 这样,就可以在默认显卡被占用(第一块显卡 ...

  8. 深入剖析tomcat的类加载机制

    1JVM类加载机制 JVM的ClassLoader通过Parent属性定义父子关系,可以形成树状结构.其中引导类.扩展类.系统类三个加载器是JVM内置的. 它们的作用分别是: 1)引导类加载器:使用n ...

  9. openwrt安装编译

    官网安装编译推荐: https://wiki.openwrt.org/doc/howto/buildroot.exigence https://wiki.openwrt.org/doc/howto/b ...

  10. LeetCode406. Queue Reconstruction by Height Add to List

    Description Suppose you have a random list of people standing in a queue. Each person is described b ...