poj 3259 Wormholes(最短路+spfa+判负回路)

【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: 22566 Accepted: 8056

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


思路:本题就是判断是否有出现负权回路。用SPFA,看是否有顶点入队数超过顶点数,有则说明有负权回路。


#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int nn=510;
const int mm=3100;
const int oo=1e9;
class node
{
  public:int v,c;
};
vector<node >e[nn];
int n,m,w,dis[nn],id[nn];
bool vis[nn];
queue<int>q;
bool spfa(int x)
{
  memset(id,0,sizeof(id));
  memset(vis,0,sizeof(vis));
  for(int i=0;i<n;i++)
    dis[i]=oo;
  dis[x]=0;vis[x]=1;++id[x];
  q.push(x);int z;
  while(!q.empty())
  {
    z=q.front();q.pop();vis[z]=0;
    for(int i=0;i<e[z].size();i++)
    {
      if(dis[e[z][i].v]>dis[z]+e[z][i].c)
      {
        dis[e[z][i].v]=dis[z]+e[z][i].c;
        if(!vis[e[z][i].v])
        {
          vis[e[z][i].v]=1;++id[e[z][i].v];
          q.push(e[z][i].v);
          if(id[e[z][i].v]>n)
            return 1;
        }
      }
    }
  }
  return 0;
}
int main()
{
  int cas;
  while(cin>>cas)
  {
    while(cas--)
    { memset(e,0,sizeof(e));
      cin>>n>>m>>w;
      int a,b,c;node z;
      for(int i=0;i<m;i++)
      {
        cin>>a>>b>>c;a--;b--;
        z.v=a;z.c=c;
        e[b].push_back(z);z.v=b;
        e[a].push_back(z);
      }
      for(int i=0;i<w;i++)
      {
        cin>>a>>b>>c;a--;b--;
        z.v=b;z.c=-c;
        e[a].push_back(z);
      }
      bool flag=0;
      for(int i=0;i<n;i++)
        if(spfa(i))
           {
             flag=1;break;
           }
        if(flag)cout<<"YES\n";
        else cout<<"NO\n";
    }
  }
}




极智嘉嵌入式面试题及参考答案 IIC(Inter-Integrated Circuit)即集成电路总线,是一种由 PHILIPS 公司开发的两线式串行总线,用于连接微控制器及其外围设备。它仅使用两根线,即串行数据线 SDA 和串行时钟线 SCL,就能实现多个设备之间的通信,具有简单、高效、占用资源少等优点,广泛应用于各种嵌入式系统中。 阅读详情

相关推荐

逆向攻防世界CTF系列40-ReverseMe-120

注意sub_401000main中只传了两个参数,但是跟进后发现四个(可能是隐式传递,放在寄存器里传递了),调试发现a4是输入长度,与最终的you_know_how_to_remove_junk_code比较的是v12(但是并没有被传入),开始怀疑是不是隐式传递了。_mm_xor_si128用于计算128位(16字节)的按位异或,然后通过v14控制循环结束的条件,可以看到v14增长的步长为16,而且通过上面得到的flag值解码得到的字符串为32个字节大小,正好是16的整数倍。

LH1013886337的博客 1410

POJ 3259 Wormholes (最短路 SPFA 判断负环)

POJ 3259 Wormholes (最短路 SPFA 判断负环) 题意: N个农场旅行 有M个路径 和 W个虫洞。 有M行 普通路径 S ,E, T。表示从地点S(E)到地点E(S)的时间为T。接着有W行为虫洞。普通路径是双向的, 虫洞是单向,且为负值 思路:既有虫洞又有普通道路,把虫洞看成是一条负权路,问题就转化成 求一个图中是否存在负权回路 。 定义一个数组记录入队次数,每个元素 入队次数是否小于 N 。

jianjun的专栏 518

ADS2020实战:手把手教你搭建交叉耦合VCO并分析相位噪声(附避坑指南)

本文以ADS2020软件为例,提供了交叉耦合压控振荡器(VCO)从原理图搭建到相位噪声分析的完整实战指南。详细讲解了核心器件选择、谐波平衡与瞬态仿真的关键设置,并重点剖析了相位噪声的仿真方法与优化方向。文中附有详细的避坑指南,帮助初学者和工程师高效完成VCO设计与仿真验证,避免常见错误。

p5l2m9n4o6q的博客 277

[POJ3259](最短路spfa)Wormholes

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

Z7784562的博客 249

嵌入式开发面试题全解析:指针、内存、数据类型关键问题总结

本文对嵌入式开发典型面试题进行深度解析,涵盖多方面关键知识。题目 19 揭示字符指针修改字符串常量的错误操作;题目 20 展现strcpy引发的缓冲区溢出风险;题目 21 体现未初始化数组与strlen结合的不可预测性;题目 22 通过位域与对齐规则计算结构体大小;题目 23 因unsigned char溢出导致循环异常。这些题目全面覆盖指针操作、内存管理、数据类型特性等核心内容,精准指出开发中易疏忽的陷阱,有助于理解编程规范,规避风险,提升代码质量与稳定性,是深入掌握嵌入式开发要点的实用指南。

嵌入式中手 1364

UOS的ARM机编译QT5.14.2记录

我需要的qt版本是Qt5.14.2,所以选择编译Qt5.14.2源码;几个月前尝试了编译一次,当时编译过程中出现了报错,当时时间很紧最后安装的默认版本来使用,最近有时间重新进行了一次编译,历经3个星期(4G的内存)终于解决了各种坑,编译成功了,现在将编译过程中遇见的一些坑记录一下,方便日后能记得这一次可恶的编译过程,如有描述欠缺的地方,还希望各位大佬给点意见指导指导🙏。

NiF693zzz的博客 1617

POJ 3259 Wormholes 最短路SPFA 判断负环

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

紫芝的博客 540

D触发器版图设计的艺术:最小化面积与最大化性能的平衡之道

本文探讨了D触发器版图设计中如何平衡最小化面积与最大化性能的核心挑战。通过分析晶体管布局、互联优化、时钟分布等关键策略,结合先进制程下的功耗与信号完整性考量,为数字IC工程师提供了从基础优化到制造验证的实用解决方案,助力实现高性能、低面积的芯片设计。

aa123的博客 910

【POJ 3259】Wormholes(最短路SPFA/Bellman_Ford)

DescriptionWhile 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 t

Ice_Confuse 1237

uniapp - 详细解决 H5 网页报错 Uncaught Error: Async component timed out after ? ms.(移动端h5页面白屏或加载缓慢失败,超时报错处理)

uni-app,vue3,网页,h5,移动端,手机H5页面,浏览器,报错,uniapp报错,Asynccomponenttimedoutafter,如何解决这个问题,vue3Asynccomponenttimedoutafter,异步组件超时了怎么办,async,详细解决方案,AsyncComponents异步组件,导致页面打不开了,白屏什么也不显示,网页载入非常慢卡顿,vue3网站报错,超时,nuxt3,详细解决方案,浏览器控制台报错,爆红并且页面卡住了,依旧报错,找不到异步组件的报错

🏆 前端领域优质创作者 3776

YOLOv13有效涨点改进专栏目录 | 本专栏持续更新500+篇内容 | 包含各种卷积、主干网络、各种注意力机制、检测头、损失函数、小目标检测改进、二次创新模块、独家创新等几百种创新点改进

《YOLOv13改进专栏重磅更新》推出500+创新改进方案,涵盖卷积、主干网络、注意力机制、检测头等核心模块优化。专栏提供完整项目代码及答疑服务,包含YOLOv13网络结构解析、环境配置、模型训练全流程指导。内容分为八大模块:卷积优化、注意力机制、细节涨点、主干网络改进、特征融合、检测头优化、小目标检测及Mamba联合改进等,支持多种创新点组合应用,助力科研论文发表。现开放限时订阅,提供试读章节和详细技术解析。

Ai缝合怪、助力小伙伴高效发论文 3382

POJ 3259 Wormholes(spfa判负权)

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

WonderKing'blog 369

032、准谐振反激变换器(QR-Flyback)原理

文章摘要: 本文以65W PD快充炸管案例切入,解析准谐振反激变换器(QR-Flyback)原理。通过主动利用漏感与MOS结电容的谐振,在Vds波谷开通实现近似ZVS,显著降低开关损耗与电压应力。对比传统反激,QR模式轻载效率提升8%,Vds尖峰减少23%。关键设计要点包括:变压器漏感需<3%、谷底检测电路需抗干扰、启动阶段需防过冲。推荐NCP1340等专用控制芯片,并强调VCC电容优选低ESR陶瓷材质。QR适用于30W-150W场景,其核心优势在于轻载效率优化,但需平衡漏感控制与可靠性。作者结合实测数据与

Jack010169的博客 114

【spfa求负权回路】poj 3259 Wormholes

题意:field块地,path条路,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。每条路给出起点、终点和经过时间,我们的任务是判断能否从某地出发,在过去的某个时间回到该地。 思路:即判断是否有负权回路,有的话就能回到原地。 关于SPFA:SPFA 在形式上和广度(宽度)优先搜索非常类似,不同的是bfs中一个点出了队列就不可能重新进入队列, 但是SPFA...

iroy33的博客 224

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

[kuangbin]POJ1860 Currency Exchange POJ3259 Wormholes | 解决正负权回路

以下是会用到题目的链接 POJ1860Currency Exchange POJ3259Wormhole 以下内容需要用到最短路的spfa算法 一、引言 在这之前的总结当中,总结过不少的最短路问题,现在又碰到了新的问题,正负权回路,为什么要用这两个题当做例题来引出来呢?是因为这两个题,一个为正权回路,另一个为负权回路,其实正负权回路的原理是一样的,不过用这两个题既能练习,又能加以区分...

一只酷酷光儿的博客 587

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

poj3259Wormholes【最短路SPFA判断负环】

Wormholes Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 56   Accepted Submission(s) : 24 Problem Description While exploring his many f

qingkong5886的博客 407

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

kuangbin专题四 F 题 负权回路(POJ 3259 Wormholes)

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

aboc43983的博客 288

POJ 3259 Wormholes SPFA 判断负权回路

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 a

旧时光 880

poj3259Wormholes【SPFA判负环】

Language: Default Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38297   Accepted: 14093 Description While exploring his many farms, Farmer Jo

走一步再走一步 681

POJ3259 Wormholes SPFA负权回路判断

题目要我们求是否存在一条路,FJ重新回到起点之后时间回到了过去,就是让我们判断在图中是否存在负权回路。 我用了SPFA,当一个点入队超过n次时,可以判断图中存在负权回路。 注意图中的前m条路是双向的,注意处理 后面的w条虫洞是单向的。 Wormholes Time Limit: 2000MS   Memory Limit: 65536K

728

Wormholes POJ - 3259(最短路,求是否存在负环)

POJ—3259 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 ti...

qq_44293044的博客 220

POJ 3259 Wormholes(Bellman_Ford判负圈)【模板】

介绍: 来自:https://www.cnblogs.com/Mr94Kevin/p/9513642.html Bellman-Ford算法是一种单源最短路算法,允许图中有负边权。Bellman-Ford算法的效率相对较低,但是很容易编写,也很好理解。比较流行的SPFA算法其实就是他的队列优化。Bellman-Ford算法的流程大体是这样,先将源点的最短路设置为0,将其他结点的最短路设置为inf...

Sean 270

20-1-10-SPFA&&bellman_ford-POJ3259

SPFA算法介绍](https://blog.youkuaiyun.com/strve/article/details/80957491) poj3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77551 Accepted: 28814 Description While explori...

shen253135371的博客 228

Wormholes(POJ 3259)

~题目链接~ http://poj.org/problem?id=3259 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 题目:输入农田个数,连通的路数,虫洞的个数。两农田之间连通是双向的,而虫洞是单向的。虫洞有这样的性质:...

ddn220120的博客 193

(POJ 3259)Wormholes 判断负环 bellman_ford 或者 spfa

Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45772 Accepted: 16904 DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing wormh

STILLxjy 566

poj 3259 Wormholes : spfa 双端队列优化 判负环 O(k*E)

1 /** 2 problem: http://poj.org/problem?id=3259 3 spfa判负环: 4 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) 5 spfa双端队列优化: 6 维护队列使其dist小的点优先处理 7 **/ 8 #include<stdio.h> 9 #include<deque&...

weixin_30383279的博客 172
上一篇: poj 3268 Silver Cow Party(最短路+SPFA)
下一篇: poj 2570 Fiber Network(最短路+Floyd)
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值