##题目描述
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

题目大意: 第一行 输入一个数 是表示几组测试数据
第二行 三个数 N(点的个数),M(正边的个数),W(负边的个数) 注意 :正边为双向的,负边为单向的。
然后 M行u,v,w;
再然后W行u,v,w;
求这个图是不是存在负环。 有 YES 没NO。

题解:思路与判断是否存在正环是一样的,我们用结构体来存u, v, w,松弛一遍判断就行

#include<iostream>
#include<stdio.h>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
struct p{int x, y, z;
}arr[1000005];
int dis[1000005];
int n, k;
bool djs()
{memset(dis, INF, sizeof dis);  //dis数组在判断负圈是否存在时,不用初始化,dis[1] = 0;                   //当然,初始化了也没有什么影响for(int i = 1; i < n; i++){  //n-1条边for(int j = 0; j < k; j++){  //松弛一边dis[arr[j].y] = min(dis[arr[j].y], dis[arr[j].x] + arr[j].z);}}for(int j = 0; j < k; j++){//如果还存在这种情况就肯定存在负圈if(dis[arr[j].y] > dis[arr[j].x] + arr[j].z)return true;}return false;
}
int main()
{int t;cin >> t;int a, b, c;int m, w;while(t--){k = 0;scanf("%d%d%d", &n, &m, &w);for(int i = 1; i <= m; i++){scanf("%d%d%d", &a, &b, &c);arr[k].x = a;arr[k].y = b;arr[k++].z = c;arr[k].x = b;arr[k].y = a;arr[k++].z = c;}for(int i = 1; i <= w; i++){scanf("%d%d%d", &a, &b, &c);arr[k].x = a;arr[k].y = b;arr[k++].z = -c;}if(djs())cout << "YES" << endl;else cout << "NO" << endl;}return 0;
}

F - Wormholes(判断是否存在负环)相关推荐

  1. POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)

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

  2. bellman ford 算法 判断是否存在负环

    Flyer 目录视图 摘要视图 订阅 微信小程序实战项目--点餐系统        程序员11月书讯,评论得书啦        Get IT技能知识库,50个领域一键直达 关闭 bellman for ...

  3. 用SPFA判断是否存在负环

    题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 ...

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

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

  5. spfa判断负环( 负环判定 + spfa )

    给定一个 n 个点 m 条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你求出 1 号点到 n 号点的最短距离,如果无法从 1 号点走到 n 号点,则输出 impossible. 数据保证 ...

  6. 解题报告:luoguP2868 Sightseeing Cows G(最优比率环,负环判定,二分答案)

    根据题意,我们要环上各点权值之和除以各边权值之和最大. 求最大答案,很明显可以使用二分答案.那么我们假设当前答案为 x,如果有更大的答案,那么方程就可以按下图转换: 也就是说如果有更大的答案,则有一个 ...

  7. P3385 【模板】负环

    https://www.luogu.com.cn/problem/P3385 这个和普通的判负环有点区别.普通的判负环是判断有没有负环即可. 这个的判负环是从1出发的负环. 故得将距离初始化为无穷,然 ...

  8. Wormholes——Bellman-Ford判断负环

    [题目描述] While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A w ...

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

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

最新文章

  1. python从入门到实践_Python编程从入门到实践日记Day32
  2. QByteArray怎么转化为QString类型,并且在QLineEdit上面显示出来
  3. SAP 调用smartforms打印如何统计实际打印状态和打印次数
  4. PostCSS自学笔记(二)【番外篇二】
  5. 常见优化Sql查询性能的方法收集
  6. [开源] FreeSql.AdminLTE.Tools 根据实体类生成后台管理代码
  7. python从图片里提取主要颜色
  8. 32 岁大佬阿里二面,他答 JVM 题的姿势,令面试官很想不通。。。
  9. 怎样给oracle表设置序列,Oracle的sequence序列在hibernate中的配置方法
  10. Spring和springMVC父子容器的原理
  11. python实现pdf到excel的自动批量转换(附 完整代码)
  12. 前端项目打包后生成的chunk-vendors文件过大,导致加载太慢
  13. 历年研究生数学建模优秀论文汇总
  14. PostgreSQL RULE
  15. Python Pandas 列数据筛选方法汇总
  16. 圭尔夫大学计算机科学硕士,加拿大硕士生物医学专业介绍:圭尔夫大学
  17. Google 搜素技巧分享
  18. 差动直流放大电路仿真 -- 单端输入,双端输入,共模,差模(附Multisim)
  19. 小米AX1800开SSH权限
  20. STM32H743必要外围电路分析

热门文章

  1. html是超文本标记语言标签,main标签
  2. 逆向破解之160个CrackMe —— 007
  3. rabbitmq之window环境启动
  4. nginx+php使用open_basedir限制站点目录防止跨站
  5. 效仿盖茨:PPstream创始人的心路历程
  6. 九度OJ 1048:判断三角形类型
  7. 未来十年,你的人生目标在哪里?
  8. 【南邮操作系统实验】页面置换算法 (FIFO、LRU、OPT)Java 版
  9. 数据库内容集锦(持续更新)
  10. 【渗透测试实战】PHP语言有哪些后门?以及利用方法