Wormholes

Time Limit: 2000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

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..N, M (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, F. F farm descriptions follow. Line 1 of each farm: Three space-separated integers respectively: N, M, and W Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) 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 (S, E, T) 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.
  1. //Memory Time
  2. //308K 204MS
  3.  
  4. #include<iostream>
  5. #include <string.h>
  6. using namespace std;
  7.  
  8. int dis[]; //源点到各点权值
  9. const int max_w=; //无穷远
  10.  
  11. struct weight
  12. {
  13. int s;
  14. int e;
  15. int t;
  16. }edge[];
  17.  
  18. int N,M,W_h; //N (1≤N≤500)fields 顶点数
  19. //M (1≤M≤2500)paths 正权双向边
  20. //W_h (1≤W≤200) wormholes 虫洞(回溯),负权单向边
  21. int all_e; //边集(边总数)
  22.  
  23. bool bellman()
  24. {
  25. bool flag;
  26.  
  27. /*relax*/
  28.  
  29. for(int i=;i<N-;i++) ///dis松弛的次数
  30. {
  31. flag=false;
  32. for(int j=;j<all_e;j++) ///所有边集
  33. if(dis[edge[j].e] > dis[edge[j].s] + edge[j].t) ///可以松弛 更新
  34. {
  35. dis[edge[j].e] = dis[edge[j].s] + edge[j].t;
  36. flag=true; //relax对路径有更新
  37. }
  38. if(!flag)
  39. break; //只要某一次relax没有更新,说明最短路径已经查找完毕,或者部分点不可达,可以跳出relax
  40. }///已经更新完毕了 所有边集都是两点之间的最短路
  41.  
  42. /*Search Negative Circle*/
  43.  
  44. for(int k=;k<all_e;k++) ///遍历所有边集 如果还出现能够再次更新成最短的边 就表明出现负权值回路了
  45. if( dis[edge[k].e] > dis[edge[k].s] + edge[k].t)
  46. return true;
  47. return false;
  48. }
  49. int main(void)
  50. {
  51. int u,v,w;
  52.  
  53. int F;
  54. cin>>F;
  55. while(F--)
  56. {
  57. memset(dis,max_w,sizeof(dis)); //源点到各点的初始值为无穷,即默认不连通
  58.  
  59. cin>>N>>M>>W_h;
  60.  
  61. all_e=; //初始化指针
  62.  
  63. /*read in Positive Paths*/
  64.  
  65. for(int i=;i<=M;i++)
  66. {
  67. cin>>u>>v>>w;
  68. edge[all_e].s=edge[all_e+].e=u;
  69. edge[all_e].e=edge[all_e+].s=v;
  70. edge[all_e++].t=w;
  71. edge[all_e++].t=w; //由于paths的双向性,两个方向权值相等,注意指针的移动
  72. }
  73.  
  74. /*read in Negative Wormholds*/
  75.  
  76. for(int j=;j<=W_h;j++)
  77. {
  78. cin>>u>>v>>w;
  79. edge[all_e].s=u;
  80. edge[all_e].e=v;
  81. edge[all_e++].t=-w; //注意权值为负
  82. }
  83.  
  84. /*Bellman-Ford Algorithm*/
  85.  
  86. if(bellman())
  87. cout<<"YES"<<endl;
  88. else
  89. cout<<"NO"<<endl;
  90. }
  91. return ;
  92. }
  93. //Memory Time
  94. //308K 204MS
  95.  
  96. #include<iostream>
  97. #include <string.h>
  98. using namespace std;
  99.  
  100. int dis[]; //源点到各点权值
  101. const int max_w=; //无穷远
  102.  
  103. struct weight
  104. {
  105. int s;
  106. int e;
  107. int t;
  108. }edge[];
  109.  
  110. int N,M,W_h; //N (1≤N≤500)fields 顶点数
  111. //M (1≤M≤2500)paths 正权双向边
  112. //W_h (1≤W≤200) wormholes 虫洞(回溯),负权单向边
  113. int all_e; //边集(边总数)
  114.  
  115. bool bellman()
  116. {
  117. bool flag;
  118.  
  119. /*relax*/
  120.  
  121. for(int i=;i<N-;i++) ///dis松弛的次数
  122. {
  123. flag=false;
  124. for(int j=;j<all_e;j++) ///所有边集
  125. if(dis[edge[j].e] > dis[edge[j].s] + edge[j].t) ///可以松弛 更新
  126. {
  127. dis[edge[j].e] = dis[edge[j].s] + edge[j].t;
  128. flag=true; //relax对路径有更新
  129. }
  130. if(!flag)
  131. break; //只要某一次relax没有更新,说明最短路径已经查找完毕,或者部分点不可达,可以跳出relax
  132. }///已经更新完毕了 所有边集都是两点之间的最短路
  133.  
  134. /*Search Negative Circle*/
  135.  
  136. for(int k=;k<all_e;k++) ///遍历所有边集 如果还出现能够再次更新成最短的边 就表明出现负权值回路了
  137. if( dis[edge[k].e] > dis[edge[k].s] + edge[k].t)
  138. return true;
  139. return false;
  140. }
  141. int main(void)
  142. {
  143. int u,v,w;
  144.  
  145. int F;
  146. cin>>F;
  147. while(F--)
  148. {
  149. memset(dis,max_w,sizeof(dis)); //源点到各点的初始值为无穷,即默认不连通
  150.  
  151. cin>>N>>M>>W_h;
  152.  
  153. all_e=; //初始化指针
  154.  
  155. /*read in Positive Paths*/
  156.  
  157. for(int i=;i<=M;i++)
  158. {
  159. cin>>u>>v>>w;
  160. edge[all_e].s=edge[all_e+].e=u;
  161. edge[all_e].e=edge[all_e+].s=v;
  162. edge[all_e++].t=w;
  163. edge[all_e++].t=w; //由于paths的双向性,两个方向权值相等,注意指针的移动
  164. }
  165.  
  166. /*read in Negative Wormholds*/
  167.  
  168. for(int j=;j<=W_h;j++)
  169. {
  170. cin>>u>>v>>w;
  171. edge[all_e].s=u;
  172. edge[all_e].e=v;
  173. edge[all_e++].t=-w; //注意权值为负
  174. }
  175.  
  176. /*Bellman-Ford Algorithm*/
  177.  
  178. if(bellman())
  179. cout<<"YES"<<endl;
  180. else
  181. cout<<"NO"<<endl;
  182. }
  183. return ;
  184. }

poj 3259 Wormholes 判断负权值回路的更多相关文章

  1. POJ 3259 Wormholes Bellman_ford负权回路

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

  2. POJ 3259 Wormholes(负权环路)

    题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...

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

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

  4. poj 3259 (Bellman_Ford判断负环)

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

  5. Wormholes POJ - 3259 spfa判断负环

    //判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...

  6. POJ 3259 Wormholes (Bellman_ford算法)

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

  7. POJ 3259 Wormholes(最短路&spfa正权回路)题解

    题意:给你m条路花费时间(双向正权路径),w个虫洞返回时间(单向负权路径),问你他能不能走一圈回到原点之后,时间倒流. 思路:题意有点难看懂,我们建完边之后找一下是否存在负权回路,存在则能,反之不能. ...

  8. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

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

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

随机推荐

  1. 纸上谈兵:图(graph)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 图(graph)是一种比较松散的数据结构.它有一些节点(vertice),在某些节 ...

  2. 移动设备页面高度不足时min-height 的尴尬处理

    移动设备页面高度不足时min-height 的尴尬处理 在做html5的页面时,经常遇到页面内容太少撑不起来整个手机屏幕的高度. 我们经常使用min-height来处理,比如min-height:56 ...

  3. Qlikview 处理增量数据的脚本

    一般设计Qlikview报表的时候需要些2个脚本文件,一个针对Qlikview的Server job 导出数据到qvd数据文具. 另一个用户访问的Qlikview的脚本是直接展示qvd文件的数据. 事 ...

  4. Python之路【第十五篇】:Web框架

    Python之路[第十五篇]:Web框架   Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 1 2 3 4 5 6 ...

  5. weed-fs参数列表

    weed-fs没有详细的帮助文档,为了方便阅读,特意把有用的参数帮助罗列出来.未列出的两个命令为version(版本查询) 及shell(这个命令在0.45版本只有回显功能)nerc@Ubuntu:~ ...

  6. xfire webServeic 例子

    xfire webServeic 例子,参考网上众多例子,自己写得完成了,给大家分享 大家只要按这个目录去建文件就可以了,然后运行,至于其中原理慢慢理会吧 环境:myeclipse 10 +xfire ...

  7. HDU 3775 Chain Code ——(Pick定理)

    Pick定理运用在整点围城的面积,有以下公式:S围 = S内(线内部的整点个数)+ S线(线上整点的个数)/2 - 1.在这题上,我们可以用叉乘计算S围,题意要求的答案应该是S内+S线.那么我们进行推 ...

  8. removeClass() 方法

    删除元素的class类别用removeClass() 方法,与addClass()方法对应.具体使用如下: 如果要删除 p 标记是 cls0 的类别,可以使用如下的代码: $("p" ...

  9. smarty基本语法

    smarty基本语法: 1.注释:<{* this is a comment *}>,注意左右分隔符的写法,要和自己定义的一致. <{* I am a Smarty comment, ...

  10. POJ 1228 - Grandpa's Estate 稳定凸包

    稳定凸包问题 要求每条边上至少有三个点,且对凸包上点数为1,2时要特判 巨坑无比,调了很长时间= = //POJ 1228 //稳定凸包问题,等价于每条边上至少有三个点,但对m = 1(点)和m = ...