POJ 3259 Wormholes Bellman_ford负权回路
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 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
Sample Input 8 Sample Output
NO
YES
题意:
John的农场里N块地,M条路连接两块地,W个虫洞;路是双向的,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。
思路:
Bellman_ford判断一下有没有负权回路就行了。
代码:
#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 0x3f3f3f3f
#define N 10100
int nodenum, edgenum, original;
typedef struct Edge
{
int u, v;
int cost;
} Edge;
Edge edge[N];
int flag;
int dis[N];
bool Bellman_Ford()
{
for(int i = ; i <= nodenum; ++i)
dis[i] = MAX;
int ok;
dis[]=;
for(int i = ; i <= nodenum - ; ++i)
{
ok=;
for(int j = ; j <= flag; ++j)
if(dis[edge[j].v] > dis[edge[j].u] + edge[j].cost)
{
dis[edge[j].v] = dis[edge[j].u] + edge[j].cost;
ok=;
}
if(ok) //优化这里,如果这趟没跟新任何节点就可以直接退出了。
break;
}
bool logo = ;
for(int i = ; i <= flag; ++i)
if(dis[edge[i].v] > dis[edge[i].u] + edge[i].cost)
{
logo = ;
break;
}
return logo;
} int main()
{
int T,num;
cin>>T;
while(T--)
{
scanf("%d%d%d", &nodenum, &edgenum, &num);
flag=;
int end,begin,power;
for(int i = ; i <= edgenum; i++)
{
scanf("%d%d%d", &begin,&end,&power);
edge[flag].u=begin;
edge[flag].v=end;
edge[flag].cost=power;
flag++;
edge[flag].u=end;
edge[flag].v=begin;
edge[flag].cost=power;
flag++;
}
for(int i=; i<num; i++)
{
cin>>begin>>end>>power;
edge[flag].u=begin;
edge[flag].v=end;
edge[flag].cost=-power;//注意这里是负的
flag++;
}
if(Bellman_Ford()==)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}
POJ 3259 Wormholes Bellman_ford负权回路的更多相关文章
- poj 3259 Wormholes 判断负权值回路
Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...
- POJ 3259 Wormholes(负权环路)
题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...
- POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- poj 3259 bellman最短路推断有无负权回路
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36717 Accepted: 13438 Descr ...
- POJ 3259 Wormholes(最短路&spfa正权回路)题解
题意:给你m条路花费时间(双向正权路径),w个虫洞返回时间(单向负权路径),问你他能不能走一圈回到原点之后,时间倒流. 思路:题意有点难看懂,我们建完边之后找一下是否存在负权回路,存在则能,反之不能. ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
随机推荐
- Vulkan Tutorial 22 Index buffer
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 在实际产品的运行环境中3D模型的数据往往共享多个三角形之间 ...
- Java 变量类型
局部变量 成员变量 类变量 Java局部变量 局部变量声明在方法.构造方法或者语句块中: 局部变量在方法.构造方法.或者语句块被执行的时候创建,当它们执行完成后,变量将会被销毁: 访问修饰符不能用于局 ...
- 15套java架构师大型分布式综合项目实战、千万高并发-视频教程
* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展 ...
- 基于node.js制作爬虫教程
前言:最近想学习node.js,突然在网上看到基于node的爬虫制作教程,所以简单学习了一下,把这篇文章分享给同样初学node.js的朋友. 目标:爬取 http://tweixin.yueyishu ...
- linux+tomcat+jdk环境验证码无法显示
我的环境配置:RHEL6.5+tomcat6+jdk1.6 今天遇到一个奇怪的现象,我的tomcat启动起来之后,网站无法显示验证码,导致无法登陆.我的tomcat启动过程是这样的: 我有一个进程守护 ...
- 【LeetCode】66. Plus One
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...
- Swift开发常用知识点
#pragma mark - as/类型转换as? / as! 需要根据前面的返回值决定 有?证明可选,可能为空:需要弱解包 没有?证明一定有值:大胆解包 as? 前面的结果是可选的 if let / ...
- python自动化运维学习第一天--day1
学习python自动化运维第一天自己总结的作业 所使用到知识:json模块,用于数据转化sys.exit 用于中断循环退出程序字符串格式化.format字典.文件打开读写with open(file, ...
- Linux之用户管理--初级上
管理用户命令汇总 命令 注释说明(特殊颜色的必须掌握) useradd增 同adduser命令,执行此命令可在系统中添加用户.(更改4个用户文件) userdel删 执行此命令可删除用户及相关用户的配 ...
- iOS多线程开发之GCD(中篇)
前文回顾: 上篇博客讲到GCD的实现是由队列和任务两部分组成,其中获取队列的方式有两种,第一种是通过GCD的API的dispatch_queue_create函数生成Dispatch Queue:第二 ...