Wormholes ---Bellman-Ford算法 最短路径 负权值回路判断 (POJ3259)

【POJ/算法】 3259 Wormholes(Bellman-Ford算法, SPFA ,FLoyd算法) Bellman-Ford算法 Bellman-Ford算法的优点是可以发现负圈,缺点是时间复杂度比Dijkstra算法高。而SPFA算法是使用队列优化的Bellman-Ford版本,其在时间复杂度和编程难度上都比其他算法有优势。 Bellman-Ford算法流程分为三个阶段: 第一步:数组Distant[i]记录从源点s到顶点i的路径长度,初始化数组Distant[n]为INF, Dis... 阅读详情

Wormholes

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 64151 Accepted: 23944

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

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

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

USACO 2006 December Gold

 

 1.如果存在从源点可达的负权值回路,则最短路径不存在,因为可以重复走这个回路,使得路径长度无穷小。
 2.思路:

    在求出distn-1[ ]之后,再对每条边<u,k>判断一下:加入这条边是否会使得顶点k的最短路径值再缩短,即判断: dist[u]+w(u,k)<dist[k] 是否成立,如果成立,则说明存在从源点可达的负权值回路。 

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
struct edges
{
	int s;
	int e;
	int t;
	edges(int ss,int ee,int tt):s(ss),e(ee),t(tt){}
	edges(){}
};
vector<edges>ed;
int dis[1006];
int f;
int n,m,w;
int Bellman_ford(int v)
{
	for(int i=1;i<=n;i++)
	   dis[i]=inf;
	dis[v]=0;
	for(int k=1;k<n;k++)
	{
		for(int i=0;i<ed.size();i++)
		{
			int s=ed[i].s;
			int e=ed[i].e;
			if(dis[e]>dis[s]+ed[i].t)
			   dis[e]=dis[s]+ed[i].t;
		}
	}
	for(int i=0;i<ed.size();i++)
		{
			int s=ed[i].s;
			int e=ed[i].e;
			if(dis[e]>dis[s]+ed[i].t)
			     return 1;
		}
		return 0;
}
int main()
{
	cin>>f;
	while(f--)
	{
		ed.clear();
		cin>>n>>m>>w;
		for(int i=0;i<m;i++)
		{
			int s,e,t;
			cin>>s>>e>>t;
			ed.push_back(edges(s,e,t));
			ed.push_back(edges(e,s,t));
		}
		for(int i=0;i<w;i++)
		{
			int s,e,t;
			cin>>s>>e>>t;
			ed.push_back(edges(s,e,-t));
		}
		if(Bellman_ford(1))
		   cout<<"YES"<<endl;
		else
		  cout<<"NO"<<endl;
	}
	return 0;
}

 

ArcGIS Pro2.5深度学习使用注意事项 ArcGIS Pro2.5深度学习使用中的注意事项1、ArcGIS Pro2.5深度学习使用中的注意事项1.1、Label深度学习样本工具参数介绍1.2、准备数据参数介绍1.3、训练模型介绍1.4、ArcGIS Pro2.5深度学习推理工具和模型对应关系介绍1.4.1、Classify Objects Using Deep Learning1.4.2、Classify Pixels Using D... 阅读详情

相关推荐

别再搞混了!MATLAB通信仿真中EbN0转SNR的保姆级公式推导与避坑指南

本文深入解析MATLAB通信仿真中EbN0与SNR转换的核心公式,揭示数字仿真与理论系统的关键差异。通过真实案例展示错误参数设置导致的隐蔽问题,并提供完整的参数自查清单和正确实现代码,帮助工程师和学生避免常见错误,确保仿真结果准确可靠。

weixin_28746223的博客 353

poj 3259 Wormholes——弗洛伊德判断是否存在负环

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 F

随意凯的博客 1150

【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘

虚拟化平台安装好之后,一般情况下,只有一个硬盘在工作,其他的外接硬盘(我的只有一个ssd硬盘,其他的均是usb外接,SATA接口的硬盘是否这样我暂时不清楚哈)没有在工作,但是一个硬盘明显不够我们使用的,所以我们需要去挂载外接的硬盘来扩容。...

Rubik Blog 4841

如何用 floyd 算法 解决从一个出发再回到这个顶点的负权值问题

1.题目引入: 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'

weixin_52914088的博客 719

Visio 2013 Professional密钥安装激活全攻略:版本匹配与故障排查

产品密钥是软件授权体系中的核心凭证,用于验证用户对软件的使用权限。在微软Office产品线中,Visio 2013 Professional采用25位字符的密钥机制,通过联网或电话方式完成激活。正确使用密钥不仅能确保软件功能完整,还能避免因授权问题导致的工作中断。对于企业IT运维和个人用户而言,掌握密钥类型辨析、安装包匹配规则以及激活失败排查方法,是保障老版本软件稳定运行的关键。本文围绕Visio 2013 Professional,详细讲解密钥输入、激活流程及常见错误代码处理,帮助读者高效完成部署与维护。

weixin_28487433的博客 450

Paraview:Python Calculator Filter

python calculator过滤器 from paraview.simple import * connection = servermanager.Connect('localhost',11111) er = servermanager.sources.EnSightreader(CaseFileName='/.../data.case') er.PointArrays = ['x_velocity','1','y_velocity','1','z_velocity','1','cp','1']

haimianjie2012的专栏 3499

F - Wormholes(判断是否存在负环)

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

逐梦er的博客 1647

【网络与爬虫 44】Canvas指纹伪造技术:突破浏览器图形渲染检测的终极指南

Canvas和WebGL指纹是现代反爬虫系统中最难绕过的检测技术之一,它们通过分析浏览器图形渲染的细微差异来识别用户设备。本文将深入解析Canvas和WebGL指纹的工作原理,探讨其在反爬虫检测中的应用,并提供多种有效的伪造和绕过策略,帮助开发者理解并应对这些高级检测技术。

熵数实验室 1213

poj 3259 Wormholes ([kuangbin带你飞]专题四 最短路练习)

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

Strive 591

MicroBlaze系列教程(8):AXI_CAN的使用

Xilinx AXI CAN IP的初始化配置,主要包括波特率、过滤器、回调函数设置等等。

whik1194的博客 4558

FanucROBOGUIDE软件二次开发:使用ROBOGUIDEAPI进行编程

菜单栏:位于界面顶部,提供文件、编辑、视图、插入、格式、工具、窗口和帮助等选项。工具栏:包含常用的工具按钮,如新建、打开、保存、运行、停止、机器人控制等。工作站视图:显示工作站的3D模型,用户可以在这里放置机器人、工件、工具和其他设备。机器人控制面板:用于控制机器人运动,包括手动移动、程序编辑和运行。程序编辑器:在这里可以编写和编辑机器人程序,支持Fanuc的R-30iB Plus和R-30iB控制柜的程序格式。状态栏:显示当前工作站的状态信息,如机器人位置、程序状态等。

kkchenjj的博客 1423

poj 3259 Wormholes spfa判断负环

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 64467   Accepted: 24039 Description While exploring his many farms, Farmer John has discovered a number of amaz...

人生如戏 232

【WRF模拟】如何查看 WPS 的输入静态地理数据(二进制格式)?

如何查看 WPS 的输入静态地理数据(二进制格式)?

WW、forever的博客 645

POJ -3259 Wormholes(Bellman-Ford 判负环)

Wormholes Time Limit: 2000MS Memory Limit: 65536K Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is...

名字太长容易被发现并打死 318

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

POJ 3259 Wormholes(基于Dijkstra的Bellman-Ford的算法---解决负权边)

题目链接:https://cn.vjudge.net/contest/314508#problem/E 翻译: 在探索他的许多农场时,农夫约翰发现了许多令人惊奇的虫洞。虫洞是非常特殊的,因为它是一条单行道,在你进入虫洞之前把你送到目的地!FJ的每一个农场都包括N(1≤N≤500)个区域,方便编号为1…N、M(1≤M≤2500)条路径和W(1≤W≤200)个虫洞。 因为FJ是一个狂热的时间旅行迷,他...

zaiyang遇见 327

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

POJ3259 Wormholes(最短路径)

题意; 有n个农场,m条双向路,之中有w个虫洞,虫洞为单向路,从起点到终点耗时反而是负数,求能否追上自己也就是判断是否有负权回路。 要点: 负权回路就是回路的权值之和为负数的意思,只要出现负权回路了,无论什么算法都是求不出最短路径的。这里学习了一下bellman—ford算法,dijkstra算法虽然很好写,但是是无法处理有负权的最短路径的,而bellman—ford算法可以算出有负权的最短

SeasonJoe的博客 453

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

*最短路径 Bellman-Ford & SPFA 算法实战

POJ - Wormholes-3259DescriptionWhile 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 it

Daioo 随笔 414

poj3259 - Wormholes (bellman_ford/spfa求负环模板题)

题目链接:http://poj.org/problem?id=3259 题意:一个农场主,有n个农场,农场之间有m条双向路,每条路有花费时间权值,有w个虫洞以路的形式给出,权值为可以回到多久之前。 思路:虫洞可以看成是一条负权路,问题就转化成求一个图中是否存在负权回路; 1.bellman_ford算法 求距离用模板就行了,怎么判断有没有负权回路? 判断边集E中的每...

agyohust791263的博客 208

poj3259 Wormholes(bellman负权处理)

思路:用bellman建立无向边,然后将虫洞的边置为负值求最短路,在进行n-1次松弛后,判断是否还能继续松弛。如果可以,则有负权回路,则可以在出发前回到原地,输出YES,否则,NO; #include #include #include #include #define N 6000//建立无向边时注意N的范围相应的扩大为二倍 int u[N],v[N],w[N],dis[N]; int n,m

Hard。 310

poj3259(Bellman_ford)

http://poj.org/problem?id=3259   Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20180 Accepted: 7159 Description While exploring his many farms, Farmer John has disc

ZhouRush 584

POJ 3259 Wormholes

链接:http://poj.org/problem?id=3259 题目: Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27825   Accepted: 10012 Description While exploring h

u010084308的专栏 858

[ACM] POJ 3259 Wormholes (bellman-ford最短路径,判断是否存在负权回路)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 Description While exploring his many farms, Farmer John has discovered a

逆风的方向 更适合飞翔 2730

Wormholes (poj 3259 SPFA || Bellman_Ford 判负环)

题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。 很久之前做的最短路,今天又写了一遍,复习了一下SPFA和Bellma_Ford,判断负权值回路。

@you! 2157

【原创】POJ 3259 Wormholes(Bellman-Ford) && 简介Bellman-Ford算法

【原创】POJ 3259 Wormholes(Bellman-Ford) && 简介Bellman-Ford算法 【原创】 题目大意 John有N个农场,一共有M条边,在农场上出现了W个虫洞(W是一条边),其中M是双向普通边,W是单向虫洞边。John穿行于农场之间每经过一条边(S到E)的时间为+T,每经过虫洞会时间倒流,经过...

swallowblank的博客 200

poj 3259 Wormholes 判断负权值回路

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...

weixin_30456039的博客 187

POJ No 3259 Wormholes Bellman-Ford 判断是否存在负图

题目:http://poj.org/problem?id=3259 题意:主要就是构造图, 然后判断,是否存在负图,可以回到原点 /* 2 3 3 1 //N, M, W 1 2 2 1 3 4 2 3 1 3 1 3 //虫洞 3 2 1 //N, M, W 1 2 3 2 3 4 3 1 8 */ ...

weixin_30810239的博客 116
上一篇: 一个人的旅行--最短路径(HDU 2066 Dijkstra实现)
下一篇: “浪潮杯”第九届山东省ACM大学生程序设计竞赛重现赛 G game (尼姆博弈)
SY_Pistachio
博客等级 码龄9年 74粉丝 272原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值