Wormholes(bellman-Ford)

【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... 阅读详情

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.
一个地图,有虫洞(可以回到过去),问有没有可能,从一个地方出发,通过虫洞,能在自己进虫洞之前看到自己。

其实说白了就是找负环,直接套模板(这里的套模板指的是套思路,而不是copy代码,模板手写更容易理解,并且这中模板一点也不复杂,没有copy必要)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int head[505],d[505];
struct Node
{
    int u,v,w,next;
}node[5010];
void add_edge(int u,int v,int w,int tot)
{
    node[tot].u = u;node[tot].v = v;
    node[tot].w = w;
    node[tot].next = head[u];
    head[u] = tot;
}
void relax(int u,int v,int w)
{
    d[v] = min(d[v],d[u] + w);
}
int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\巍巍\\Desktop\\in.txt","r",stdin);
    //freopen("C:\\Users\\巍巍\\Desktop\\out.txt","w",stdout);
    #endif // LOCAL
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,w;
        scanf("%d%d%d",&n,&m,&w);
        for(int i = 1;i <= n;i++)head[i] = -1;
        for(int i = 1;i <= m;i++)
        {
            int from,to,wei;
            scanf("%d%d%d",&from,&to,&wei);
            add_edge(from,to,wei,2*i - 1);
            add_edge(to,from,wei,2*i);
        }
        for(int i = 1;i <= w;i++)
        {
            int from,to,wei;
            scanf("%d%d%d",&from,&to,&wei);
            add_edge(from,to,-wei,2*m + i);
        }
        for(int i = 1;i <= n;i++)d[i] = inf;
        d[1] = 0;
        for(int i = 1;i < n;i++)
            for(int j = 1;j <= 2*m + w;j++)
            relax(node[j].u,node[j].v,node[j].w);
        bool flag = 0;
        for(int i = 1;i <= 2*m + w;i++)
        {
            int from = node[i].u;
            int to = node[i].v;
            if(d[to] > d[from] + node[i].w)
            {
                flag = 1;break;
            }
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
从波形图到参数调优:一次无感BLDC电机启动失败的深度解析 本文深入解析无感BLDC电机启动失败案例,通过波形分析揭示参数不匹配和IPM保护机制触发等关键问题。详细探讨了过零点检测、PWM周期设置及强拖参数调优的底层逻辑,并分享IPM模块保护机制与实战调优技巧,最终实现99.98%的启动成功率。 阅读详情

相关推荐

双模音频解码器设计:USB+蓝牙LDAC平衡输出方案

音频解码器是数字音频系统中实现数模转换的核心组件,其性能直接决定音质保真度与系统延迟。基于高集成SoC的解码方案通过统一时钟域与片内DSP处理,有效抑制Jitter并简化硬件拓扑;结合全平衡模拟输出架构,可显著提升共模抑制比(CMRR)与信噪比(SNR)。该技术路径广泛应用于便携式Hi-Res音频设备、USB-C小尾巴DAC及蓝牙高解析耳机放大器等场景。本文围绕ES9038Q2M双模SoC展开,详解其在LDAC协议支持、UAC2.0兼容性及平衡电流型DAC输出方面的工程实现,为高性能、可量产音频硬件开发提供

weixin_42613018的博客 387

POJ 3259 Wormholes (bellman_ford)

题目链接:~( ̄▽ ̄~)(~ ̄▽ ̄)~ 资料链接:\(▔▽▔)/ code: #include int n = 0, m = 0, w = 0, count = 0; typedef struct { int u, v, weight; }node; node edge[6000]; int bellman_ford() { int i = 0, j = 0, d

Cifer 473

从翼型到湍流:用生活化案例图解空气动力学核心概念(中英双语版)

本文通过飞机机翼、F1赛车、高尔夫球等生活化案例,图解空气动力学核心概念。深入浅出地解释了攻角如何产生升力、边界层与流动分离如何影响阻力、马赫数如何划分速度世界,以及涡流与湍流的特性与应用。文章还介绍了计算流体动力学(CFD)这一现代数字风洞工具,帮助读者直观理解这些塑造现代交通与运动设计的无形之力。

metaverse5vr的博客 475

poj3259 Wormholes floyd求负环

POJ 3259 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞。虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞。FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的那个区域出发前的时间。也许他就能遇到

yslcl12345的博客 5111

多智能体系统在AI物流优化中的实际应用案例

订单波峰波谷难应对(如双11订单量暴增300%);动态干扰难处理(如突发交通拥堵、客户临时改地址);全局优化难实现(单辆车最优≠整体最优)。本文聚焦“多智能体系统(MAS)”这一AI技术,讲解其如何通过“群体智能”破解物流调度难题,并结合实际案例展示落地效果。本文从“生活故事→核心概念→技术原理→实战案例→未来趋势”逐步展开,用“快递员团队协作送外卖”类比MAS运作,结合Python代码模拟任务分配,最后通过某物流企业的真实项目验证效果。

AIGC应用创新大全的博客 1630

2025高教社数学建模国赛A题 - 烟幕干扰弹的投放策略(完整参考论文)

摘要:本研究基于模拟遗传退火算法优化无人机投放烟幕弹策略,建立三维运动学模型分析遮蔽效果。针对不同战术场景(单机单弹至多机多弹),构建以遮蔽时间最大化为目标的非线性优化模型。通过改进遗传算法求解最优参数组合,结果显示在给定条件下单枚烟幕弹对M1导弹的有效遮蔽时长可达1.4秒。研究为无人机协同干扰提供了高效解决方案,验证了智能算法在复杂战术决策中的适用性。关键词:烟幕干扰;无人机投弹;运动学建模;时序优化;遗传算法。

ProminentWu的博客 544

Wormholes(Bellman-ford)

Wormholes Time Limit:2000MS Memory Limit:65536K Total Submissions:33008 Accepted:12011 Description While exploring his many farms, Farmer John has discovered...

weixin_30764137的博客 122

20 Embedding 与向量检索:语义为什么能变成距离

Embedding 是 RAG 语义检索的数学基础。本文用工程视角讲清文本如何变成向量,为什么语义相近会表现为距离相近,Top-K 检索如何工作,以及在模型选择、同模型约束、精确匹配和召回评估上的常见坑。

老于的笔记的专栏 6202

POJ 3259 Wormholes(最短路——Bellman-ford)

A - 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 that is BEFORE you entered the wormhole! Each of

沐雨风栉 365

【ComfyUI专栏】ComfyUI 数字人方案-ComfyUI-EchoMimic

EchoMimic是一个基于图片和语音的数字人生成方案,通过ComfyUI节点实现。该方案安装简单,需下载特定模型文件(Unet、AudioProcessor和VAE)并保存本地。相比其他数字人生成方案,其节点操作更简便,支持多种Pose姿态选择,并能结合视频内容进行姿态同步,通过音频同步口型实现动态内容创建。生成的数字人效果直观,适用于快速创建个性化数字形象。

技术与管理的平衡木- 464

PCB板Gerber文件的理解和制作

Gerber文件是PCB生产的关键文件,主要包括:1)走线层(GTL/GBL);2)阻焊层(GTS/GBS);3)丝印层(GTO/GBO);4)板框层(GMx/GKO);5)钢网层(Gtp/GBp);6)钻孔文件(NCdrillFiles)。其中1-10项和15项钻孔文件必须提供,建议包含11-14项辅助钻孔层。Gerber文件不含过孔属性,需提前在AD中设置过孔盖油。导出步骤包括:导出非机械层、机械层和钻孔层,新版AD可通过"文件->制造输出->GerberFiles"操作

qq_37999850的博客 852

Wormholes(Bellman-Ford找负权回路)

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

sdau20163940的博客 385

基于STM32L4XX 、HAL库的SGM452B数字温度传感器驱动应用C语言程序设计

SGM452B_RESOLUTION_12BIT = SGM452B_CONFIG_R1 | SGM452B_CONFIG_R0 /* 12位分辨率,精度0.0625°C,转换时间200ms */SGM452B_RESOLUTION_11BIT = SGM452B_CONFIG_R1, /* 11位分辨率,精度0.125°C,转换时间100ms */SGM452B_RESOLUTION_10BIT = SGM452B_CONFIG_R0, /* 10位分辨率,精度0.25°C,转换时间50ms */

PP_Cottage的博客 327

【POJ 3259 --- Wormholes】Bellman-Ford, 负权回路

【POJ 3259 --- Wormholes】Bellman-Ford, 负权回路 题目来源:点击进入【POJ 3259 — Wormholes】 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very pecu...

来日浅谈的博客 514

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

POJ 3259 Wormholes【Bellman-Ford】

http://poj.org/problem?id=3259POJ 3259 Wormholes大意:给出由n个顶点组成的有向图所有边信息,判断该图中是否存在负回路分析: 由于不知道图中的负环路会包含哪些顶点,故自定义一源点s,对于每个顶点k,有一条s到k权值为0的有向边,此时便可初始化s到每个顶点的距离dis[k]为0,然后用Bellman-Ford计算从源点s到各点的単源最短路,若...

a48634331的博客 161

ssm框架搭建

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

poj 3259 Wormholes(Bellman-Ford)

poj 3259 WormholesDescriptionWhile 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

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 900

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

uva 558 - Wormholes(Bellman Ford判断负环)

题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环。 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能更新点的权值,则说明有负环的存在。 #include #include #define min(a,b) (a)<(b)?(a):(b) const int N

不慌不忙、不急不躁 1652

pku 3259 Wormholes (Bellman ford判断负权边)

题目链接:  pku3259 Wormholes    之前在数据结构书上看了dijkstra 和floyed(从来没有自己写过)   但是有负权边的一直不会   所以专门找了题目看看怎么写   感觉上bellmanford 跟 dijkstra 处理问题的关键步骤差不多 都是不断的松弛   但是bellmanford顺序不同 dijkstra是对每个点进行松弛

Time flies 864

【原创】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

Wormholes(Bellman-ford或Spfa)

Wormholes(Bellman-ford) 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 wormhol

Anthcony的博客 379

POJ 3259 Wormholes Bellman-Ford找负环

题目来源:POJ 3259 Wormholes 题意:某人想回到过去 输入有2中类型的边 一种是正的 并且是双向的 另外一种是负的 是单向的 可以回到过去就输出YES 否则NO 思路:和UVa 558差不多 如果存在负环就可以通过这个负环一直无限时光倒流 找负环用SPFA或者Bellman-Ford #include #include #include using namespace

嗯。 1239

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

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

豆子 207

Wormholes (Bellman_Ford)

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

YOONGI 379

【题解】Wormholes POJ - 3259 (Bellman-Ford判断负环)⭐⭐⭐

【题解】Wormholes POJ - 3259 ⭐⭐⭐ 农夫约翰在探索他的许多农场,发现了一些惊人的虫洞。虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1…N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞。FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的...

Suprit's blog 764

POJ3259 Wormholes————Bellman-Ford

题解:本题主要考查Bellman-Ford求负环。 简要题意:N个点的图,2M条正权双向边,W条负权单向边。问图中是否存在负环? 1.Bellman-Ford:求负环用Bellman-Ford就行了,加上优化没有松弛则提前退出循环,减少时间。 代码如下: #include<iostream> #include<cstring> using namespace std; st...

wly1127的博客 178

POJ 3259 Wormholes(Bellman-Ford)

题目网址:http://poj.org/problem?id=3259 题目: Wormholes Time Limit:2000MS Memory Limit:65536K Total Submissions:52198 Accepted:19426 Description While exploring his m...

weixin_30325793的博客 88
上一篇: Currency Exchange(bellman-Ford)
下一篇: MPI Maelstrom(最短路)
Techay
博客等级 码龄11年 11粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值