poj 3259(bellman最短路径)[通俗易懂]

poj 3259(bellman最短路径)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30169   Accepted: 10914

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

AC代码:

#include<iostream>using namespace std;struct Point{    int s,e,t;}a[10000];int se;int n,m,w;int bell_man(int start){    int dis[10000];    for(int i=1;i<=n;i++)        dis[i]=999999;    dis[start]=0;    for(int i=1;i<n;i++)    for(int j=0;j<se;j++)        dis[a[j].e] = dis[a[j].e] > dis[a[j].s] + a[j].t ? dis[a[j].s] + a[j].t : dis[a[j].e];    for(int i=0;i<se;i++){        if(dis[a[i].e] > dis[a[i].s] + a[i].t)            return 1;    }    return 0;}int main(){    int T; cin>>T;    while(T--){        se=0;        cin>>n>>m>>w;        for(int i=0;i<m;i++){            int s,e,t;            cin>>s>>e>>t;            a[se].s=s; a[se].e=e; a[se++].t=t;            a[se].s=e; a[se].e=s; a[se++].t=t;        }        for(int i=0;i<w;i++){            int s,e,t;            cin>>s>>e>>t;            a[se].s=s; a[se].e=e; a[se++].t=-t;        }        //int k;        //for(k=1;k<=n;k++){          //事实上正确的起点应该要历遍全部点。可是这种超时了                                     //这个题目仅仅要1点就能够了。算是题目的一个非常大漏洞吧,数据太水了            if(bell_man(1)){                cout<<"YES"<<endl;                //break;            }        //}        //if(k>n)        else            cout<<"NO"<<endl;    }    return 0;}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116729.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月16日 下午7:00
下一篇 2022年1月16日 下午8:00


相关推荐

  • 推荐CSDN排名前1000博主

    推荐CSDN排名前1000博主推荐 CSDN 排名前 1000 博主 https blog csdn net ZYC88888 article details utm medium distribute pc relevant bbs down none task2 all first rank v2 rank v29 23 nonecase amp depth 1 utm source distribute pc relevant bbs down none task2 all first rank v2

    2026年3月18日
    4
  • 手机app测试流程和方法_app测试方法和流程

    手机app测试流程和方法_app测试方法和流程1 APP测试基本流程1.1流程图1.2测试周期测试周期可按项目的开发周期来确定测试时间,一般测试时间为两三周(即15个工作日),根据项目情况以及版本质量可适当缩短或延长测试时间。正式测试前先向主管确认项目排期。1.3测试资源测试任务开始前,检查各项测试资源。–产品功能需求文档;–产品原型图;–产品效果图;–行为统计分析定义文档;–测试设备(i…

    2025年9月16日
    5
  • python后端常见架构_常见的后端框架

    python后端常见架构_常见的后端框架后端vs前端如果您是Web开发世界的新手,后端和前端开发之间的区别可能不那么明显,但是,了解两者之间的区别很重要。以下是前端开发人员与后端开发人员的一些区别。前端开发:前端开发人员在很大程度上负责用户所看到的内容(即网站页面),前端开发人员主要使用HTML,CSS和JavaScript。他们的主要关注点是创建出色的用户体验,并确保网站设计和布局或Web应用程序始终具有凝聚力。后端开发:另一方面,后…

    2022年6月10日
    77
  • IoC控制反转是什么意思?[通俗易懂]

    IoC控制反转是什么意思?[通俗易懂]最近由于日本项目的需要,开始学习Spring框架的东西。虽然框架被日方公司进行了一定的修改,但Spring大体原理是不变的。Spring最大的特点,相信大家在网上看了许多,都知道是控制反转(IOC),或者叫依赖注入(DI),那么究竟什么是控制反转,什么是依赖注入呢?IOC(inversionofcontrol)控制反转模式;控制反转是将组件间的依赖关系从程序内部提到外部来管理; DI(depe…

    2022年6月29日
    26
  • 文件或目录损坏且无法读取怎么删除文件或目录

    文件或目录损坏且无法读取怎么删除文件或目录解决方法有几种 1 尝试为文件重命名 如果可以重命名的话 运行 cmd 打开任务管理器 结束 explorer 进程 切换到 cmd 命令提示符状态下输入 Del 文件名 后就可以删除文件了 这种方法只适用于可以重命名的文件 在进行操作时先关闭其他一切不相关的程序 2 如果无法重命名 可以试着在电脑管家或者 360 等杀毒软件中找到文件粉碎 将该文件粉碎 3 如果杀毒软件也无法将如软件删除 可以选择修复该软件所在盘符 比如我的软件在 E 盘 打开计算机 选择 E 盘 右击选择属性 选择上方的工具 点击检查按钮 等待该盘符检

    2026年3月17日
    1
  • 腾讯元宝更新版本:DeepSeek和混元两大模型均能理解图片信息

    腾讯元宝更新版本:DeepSeek和混元两大模型均能理解图片信息

    2026年3月12日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号