【题目描述】

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

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

【题目分析】
很正常的判断负环,而且就算有重边也并不影响。
【AC代码】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;typedef long long ll;
const int MAXN=505;
const int MAXM=6005;
struct node
{int u,v,w;
}edge[MAXM],E;
int tot;
int n,m,k;
int dis[MAXN];inline void AddEdge(int u,int v,int w)
{edge[tot].u=u; edge[tot].v=v; edge[tot].w=w;tot++;
}bool Bellman()
{memset(dis,0x3f,sizeof(dis));dis[1]=0;bool flag;int T=n;while(T--){flag=false;for(int i=0;i<tot;i++){E=edge[i];if(dis[E.v]>dis[E.u]+E.w){dis[E.v]=dis[E.u]+E.w;flag=true;}}if(!flag) break;}return T==-1;   //T的值最后是-1,我还以为是0.。。。
}int main()
{int T,u,v,w;scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&k);tot=0;for(int i=0;i<m;i++){scanf("%d%d%d",&u,&v,&w);AddEdge(u,v,w); AddEdge(v,u,w);}for(int i=0;i<k;i++){scanf("%d%d%d",&u,&v,&w);AddEdge(u,v,-w);}if(Bellman()){printf("YES\n");}else{printf("NO\n");}}return 0;
}

Wormholes——Bellman-Ford判断负环相关推荐

  1. POJ 3259 Wormholes【最短路/SPFA判断负环模板】

    农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...

  2. POJ - 3259 Wormholes(判断负环)

    题目链接:点击查看 题意:最短路判断负环 这里介绍三种方法判断负环,分别是spfa,bellman-ford和flyod算法,不过spfa的速度能比bellman-ford的速度慢了接近20倍是出乎 ...

  3. POJ3259(Wormholes) 判断负环

    题意: 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W个 虫洞组成,FJ想从一块土地开始,经过若干条路和虫洞 ...

  4. 模板 - 判断负环(超时高效优化技巧)、01分数规划

    整理的算法模板合集: ACM模板 判断负环 判正环求最长路,判负环求最短路 int n; // 总点数 int h[N], w[N], e[N], ne[N], idx; // 邻接表存储所有边 in ...

  5. spfa 判断负环 (转载)

    当然,对于Spfa判负环,实际上还有优化:就是把判断单个点的入队次数大于n改为:如果总的点入队次数大于所有点两倍 时有负环,或者单个点的入队次数大于sqrt(点数)有负环.这样时间复杂度就降了很多了. ...

  6. LightOJ - 1074 Extended Traffic(最短路+判断负环)

    题目链接:点击查看 题目大意:给出n个城市,每个城市都有一个拥挤度,从a到b的时间是(b的拥挤度-a的拥挤度)^3,点1为起点,求最短时间 题目分析:这个题第一感觉是个裸题,n还非常小,偷了个懒上了一 ...

  7. UVA 558 SPFA 判断负环

    这个承认自己没看懂题目,一开始以为题意是形成环路之后走一圈不会产生负值就输出,原来就是判断负环,用SPFA很好用,运用队列,在判断负环的时候,用一个数组专门保存某个点的访问次数,超过了N次即可断定有负 ...

  8. 负环——spfa判断负环的两种方式

    第一种:(不推荐) 统计每个点的入队次数,如果某一个点入队了n次,则说明存在负环. 第二种: 统计当前每个点的最短路的边数,如果存在负环,负环上的某一个点的最短路边数一定会是正无穷,只要边数超过n(节 ...

  9. AcWing 852. spfa判断负环(spfa or bellman)

    题目链接 https://www.acwing.com/problem/content/description/854/ 思路 思路一 我们定义一个数组cnt,cnt[i]表示i这个点被更新的次数,那 ...

最新文章

  1. [zt]如何用Javascript获得TextArea中的光标位置
  2. invokeRequired属性和 invoke()方法
  3. 数字溢出为啥程序出错
  4. SSM+BJUI实现CRUD的报表功能
  5. Matlab | MATLAB编辑器:无法使用GBK编码保存文件,请改用UTF-8编码保存文件(问题解决)
  6. Python精通-Python集合操作详解
  7. html只读下拉框,Html.DropDownList – 禁用/只读
  8. lottie-android: 【Android】开源动画库(Airbnb开源)
  9. JAVA 垃圾收集监控
  10. Python嗅探socket
  11. Leetcode每日一题:面试题16.19.水域大小
  12. Python容器专题 - 列表(list)
  13. spingbot 与 activiti 整个 中创建表而找不到表的问题(创建表失败)
  14. 模拟生命_吸烟致癌?
  15. IT技能图谱(图谱+干货)
  16. php微信h5支付对接流程,微信H5支付接口开发的流程与常见问题
  17. 微信小程序服务端调用--小程序码 wxacode.getUnlimited 接口调用,实现微信扫码直接跳转小程序页面
  18. 修改docker时区为北京时间
  19. centos 6.4 thinly-provisioned
  20. java任务队列_java 任务队列

热门文章

  1. win主机ping不通linux的IP
  2. Linux中vim编辑器的缩进的功能键
  3. tensorflow mnist read_data_sets fails
  4. 用Fragment制作的Tab页面产生的UI重叠问题
  5. SQLSERVER如何获取一个数据库中的所有表的名称、一个表中所有字段的名称
  6. uva 12442 . Forwarding Emails
  7. android:configChanges属性总结
  8. [转载]十四步实现拥有强大AI的五子棋游戏
  9. 空军军医大学计算机复试线,空军军医大学2019年考研复试分数线
  10. java setcontenttype_response.setContentType()在Java过滤器中重置