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次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
随机推荐
- MongoDB--操作符
$gt -- > $lt -- < $gte -- >= $lte -- <= $all 与 in 类似,不同的是必须满足[]内所有的值 $exists 字段是否存在 db.s ...
- SSH工作原理图
一个请求在Struts2框架中的处理大概分为以下几个步骤 : 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2 这个请求经过一系列的过滤器(Filter)(这些过滤器中有一个叫 ...
- php的laravel数据库版本管理器migration
第一步:连接数据库 打开.env文件.配置DB_HOST DB_PORT DB_DATABASE=LARAVEL DB_USERNAME DB_PASSWORD 注意DB_DATABASE这一项需要自 ...
- Linux之通配符
前言:学习通配符有点为正则表达式打基础的感觉……之前学python有学过正则表达式,所以这篇博客学起来还是挺快的. 特殊符号 | #管道符,或者(正则) > #输出重定向 >> #输 ...
- Spring MVC 项目搭建 -4- spring security-添加自定义登录页面
Spring MVC 项目搭建 -4- spring security-添加自定义登录页面 修改配置文件 <!--spring-sample-security.xml--> <!-- ...
- Idea调试显示切换数据源的设置
使用IDEA调试时,如果遇到相同方法会在编辑器上提示切换到哪个项目,因为手滑点了Disable,所以导致后来就不提示了,记录下设置方法.
- 不借助第三方网站四步实现手机网站转安卓APP
今天本来是帮朋友查看是否在APP里可以点外链的一个测试,做着做来感觉了,就把这个测试优化了一下.好了我们来进入正题. 工具:Android Studio 第一步:新建项目 第二步:拖入控件(WebVi ...
- Vue.js 基本功能了解
一.写在前面 隔了这么久才来出Vue的第二篇文章,真是堕落了,自己先惩罚下/(ㄒoㄒ)/~~ 回过头看自己第一篇相关文章<初试 Vue.js>(http://www.cnblogs.com ...
- Linux 密钥验证
服务端配置puttygen 生成公钥,保存私钥复制公钥,写入服务端mkdir /root/.sshvi /root/.ssh/authorized_keys 生成文件,将公钥写入到文件中chmod 7 ...
- Jemeter基础
jemeter主要组件: a.测试计划(Test Plan) 是使用JMeter进行测试的起点,它是其它JMeter测试元件的容器. b.线程组(Thread Group) 代表一定数量的并发用户,它 ...