Wormholes(POJ-3259)

POJ 3259 Wormholes 【Floyd判断负环】 POJ 3259 Wormholes 【Floyd判断负环】 题目大意: FJ 有 N 块田野【编号 1…n】田野间有 M 条路【双向】 同时有 W 个虫洞,通过这个虫洞到达另一个田野,可以回到一段时间以前【单向】 问:FJ 是否能在田地中遇到以前的自己 具体思路: 双向路为正权,单向路为负权,判断图中是否存在负环,存在则可以遇到以前的自己,至于为什么上一篇博客有解释 可用spfa求解,但是spf... 阅读详情

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

题目大意:有f个农场,每个农场有n个虫洞,m条双向通道,w条负权单向通道,问是否能回到从前?;

思路:题目即要我们判断是否有无负权环,用Bellman-ford算法;

代码如下:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int inf=0x3f3f3f3f;
int n,m,w,dist[510];
struct edge
{
    int s,e,d;
    edge(int a,int b,int l):s(a),e(b),d(l){};
};
vector<edge>v;
bool bellman_ford()
{
    memset(dist,inf,sizeof(dist));
    dist[1]=0;
    for(int i=1;i<n;i++)
    {
        bool flag=true;
        for(int j=0;j<v.size();j++)
            if(dist[v[j].s]+v[j].d<dist[v[j].e])
            {
                dist[v[j].e]=dist[v[j].s]+v[j].d;
                flag=false;
            }
        if(flag)
            return false;
    }
    for(int j=0;j<v.size();j++)
        if(dist[v[j].s]+v[j].d<dist[v[j].e])
            return true;
    return false;
}
int main()
{
    int f;
    cin>>f;
    while(f--)
    {
        int a,b,d;
        v.clear();
        cin>>n>>m>>w;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>d;
            v.push_back(edge(a,b,d));
            v.push_back(edge(b,a,d));
        }
        for(int i=0;i<w;i++)
        {
            cin>>a>>b>>d;
            v.push_back(edge(a,b,-d));
        }
        if(bellman_ford())
            cout<<"YES\n";
        else
            cout<<"NO\n";
    }
    return 0;
}

Tecplot进阶:巧用公式与多Frame对比,实现CFD多工况数据差异的可视化分析 本文详细介绍了如何利用Tecplot进行CFD多工况数据差异的可视化分析,通过公式计算和多Frame对比,实现变量差值自动计算、多视图同步展示和差异量化统计。文章还提供了数据准备、基础操作、实战技巧和常见问题解决方案,帮助工程师高效完成CFD后处理工作。 阅读详情

相关推荐

最优化问题——一维搜索(一)

最优化问题——一维搜索 1. 一维搜索的引入 1.1 从非线性规划问题到一维搜索 在前面的文章中,我们提到了基于迭代的非线性规划问题的求解思路。在求解过程中,我们提到了在每一步的迭代中都需要对计算步长因子,其计算公式为: λk=minλf(xk+λp)λ_k=min_{λ}f(x_k+λp)λk​=minλ​f(xk​+λp) 我们将f(xk+λp)f(x_k+λp)f(xk​+λp)视为λ的一元函数,我们的目标就是求这个一元函数的极小值。对于这个问题,我通常的求解方式为一维搜索。 1.2 一维搜索的基本形

hei653779919的博客 7362

POJ 刷题系列:3259. Wormholes

POJ 刷题系列:3259. Wormholes传送门:3259. Wormholes题意: N个顶点,任意顶点之间存在双向边,权重为正,也可能存在单向边(虫洞),权重为负。给出一张图,问是否存在负环。 思路:重新回顾了下Bellman-Ford算法,核心思路如下:从单个源点出发,如果经过N轮还在更新时,说明存在负环。证明:假设不存在负环,那么最短路径必然不会经过一个顶点两次,所以最多通过N-

Demon-初来驾到 894

视网膜血管分割数据集(DRIVE数据集,STARE数据集,REFUGE数据集,CHASE_DB1数据集,DiaRetDB1数据集)

在医学图像处理领域,视网膜图像分析对于眼部疾病的早期诊断与治疗具有举足轻重的意义。随着人工智能技术的蓬勃发展,尤其是深度学习算法在图像分割任务中的广泛应用,高质量的视网膜图像数据集成为推动该领域研究进展的关键要素。这些数据集不仅是训练和优化算法模型的基石,更是衡量算法性能优劣的重要依据。本文聚焦于视网膜图像分割领域,详细介绍 DRIVE、STARE、REFUGE、CHASE_DB1 以及 DiaRetDB1 这几个具有代表性的数据集。

weixin_43518285的博客 2274

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

普冉PY32F003定时器实战:从HSE时钟配置到500ms精准闪烁LED(附完整代码)

本文详细解析了普冉PY32F003单片机定时器的实战应用,从HSE时钟配置到实现500ms精准LED闪烁。通过完整的代码示例和深入的技术分析,帮助开发者掌握定时器参数计算、中断优先级管理及调试技巧,适用于各类嵌入式开发项目。

weixin_30608503的博客 453

Android无线调试:adb tcpip连接实战指南

本文详细介绍了Android无线调试的完整流程,重点讲解如何使用adb tcpip命令通过Wi-Fi连接设备,摆脱数据线束缚。涵盖环境配置、连接实战、稳定性优化及常见问题排查,帮助开发者高效实现无线adb调试,提升工作效率。

pink的博客 724

如何用 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的博客 718

MicroPython跨平台移植实战:从源码解析到STM32+FreeRTOS部署

本文详细解析了MicroPython的跨平台移植实战,重点介绍了如何将其从源码开始,成功部署到STM32微控制器并运行于FreeRTOS实时操作系统。内容涵盖源码结构解析、编译环境搭建、内存管理协调、FreeRTOS任务集成、数学库(math)启用与优化,以及自定义模块开发等核心步骤,为开发者提供了一套完整的、可操作的嵌入式Python移植方案。

vodka的博客 1014

【POJ No. 3259】虫洞 Wormholes

【POJ No. 3259】虫洞 Wormholes

代码改变世界 656

vue微前端qiankun框架学习到项目实战,基座登录动态菜单及权限控制

技术无关,独立开发,独立部署,增量升级,独立运行,微前端的核心目标是将巨石应用拆解成若干可以自治的松耦合微应用。

前端开发工程师 9754

初识colmap

COLMAP是一款开源的计算机视觉软件,主要用于从一系列二维图像中进行三维重建。它全称为“COLLISION-MAPping”,是一个高度集成的Structure-from-Motion (SfM) 和 Multi-View Stereo (MVS) 管道工具,由斯坦福大学开发和维护。COLMAP的主要功能包括特征检测与匹配、增量式SfM、多视图立体匹配以及优化。它能够从无序或有序的二维照片集合中恢复出三维场景的几何结构(点云)以及每张图像对应的相机姿态。

Hhsjehejj的博客 5556

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的博客 1646

NEU-DET 冷轧钢缺陷检测:基于 YOLOv8 的目标检测与可视化 UI 实现

冷轧钢的生产过程中,由于设备故障、工艺问题或者人为因素,钢板表面可能会出现多种缺陷,如划痕、裂纹、孔洞等,这些缺陷会严重影响钢材的质量。它比之前的版本(如 YOLOv5、YOLOv4)在速度和精度上都有显著提升,并且对硬件要求较低,适合进行工业界面和嵌入式系统的应用。这些版本相较于更大型的模型(如 YOLOv8x)具有更快的推理速度和较低的资源占用,适合工业生产线中的实时应用。通过将训练好的模型部署到工业生产线摄像头中,我们可以实现实时的缺陷检测,极大地提升生产效率和产品质量。

YOLO 740

POJ3259——Wormholes

题目链接:http://poj.org/problem?id=3259 Wormholes 题目大意:给你n个点,m条双向联通的权值为正的路,和w条单向联通权值为负的路。让你判断是否存在负环。 用队列实现,判断是否存在负环。 #include #include #include #include #define INF 0xfffffff using namespace std; c

piaocoder 400

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

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

Statistics and Machine Learning Toolbox

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

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

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

POJ 3259 Wormholes

POJ 3259 Wormholes[★★☆☆☆]图论 最短路 bellman 题目大意:本质就是求该图是否存在负环。也就是如何求出一个图是否含有负环。 要注意的是输入该题中的path是双向的,wormhole是单向的 样例输入: 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输出: NO YES 解题思路:这

Stay hungry... 810

ssm框架搭建

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

[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的博客 250

【POJ 3259】Wormholes

【POJ 3259】Wormholes 判断负环——BellMan/SPFA代码如下BellMan-Ford

ACMer' 882

POJ3259 Wormholes Floyd判负圈

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

Bill_Utada的成长之路 1203

POJ3259 Wormholes 【spfa判负环】

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

a6334167的博客 153

Wormholes POJ - 3259【最短路】

Wormholes POJ - 3259 题目 思路:能见到几秒前的自己说明存在负环,直接spfa判断负环,记得每次都把数组邻接表…之类的初始化 注意:起点不一定是从1开始的,所以每个点都应该作为入队起点 具体代码如下 #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int N = 510, M = 6000;

qq_50702668的博客 203

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

POJ 3259—Wormholes

题下:POJ 3259—Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 66356 Accepted: 24748 Description While exploring his many farms, Farmer John has discovered a number of amazing wor...

wanglin007 310

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

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

jianjun的专栏 518

POJ-3259-Wormholes

POJ-3259-Wormholes http://poj.org/problem?id=3259 bellman_ford判断是否有负环 #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;cstdlib&gt; using namespace std; #define M...

java开发指南博客 【转载】 182
上一篇: 漫步校园(HDU-1248)
下一篇: 牛客练习赛12
绿蚁.r
博客等级 码龄10年 4粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值