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次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
随机推荐
- 教你轻轻松松入门PHP
入门PHP不用愁,跟我来学一学. 1.我们想学习PHP,首先就要了解PHP,那PHP是什么呢? PHP("PHP: Hypertext Preprocessor",超文本预处理器的 ...
- MYSQL数据类型和where条件
MySQL中常见的数据类型 一.字符型 ① CHAR(N):固定N个字符长度的字符串,如果长度不够自动空格补齐; N的范围 0~255 ② VARCHAR(N): 存储可变长度的字符串,最常用 ③ T ...
- JAVA反射原理
什么是反射? 反射,一种计算机处理方式.是程序可以访问.检测和修改它本身状态或行为的一种能力.java反射使得我们可以在程序运行时动态加载一个类,动态获取类的基本信息和定义的方法,构造函数,域等.除了 ...
- 小程序之发起请求 wx.request(object)的坑
这是官方的API,然后官方的实例中 wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' , y: '' }, header: { ...
- 网络组Network Teaming
网络组team:是将多个网卡聚合在一起,从而实现容错和提高吞吐量 1 创建网络组接口 nmcli connection add type team con-name TEAMname ifname I ...
- js动态增加秒数(自动,手动)
//获取当前的日期及时间Date var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.get ...
- Android studio出现Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Reques的解决办法
最近更新了一下Android Studio,在导入新项目之后出现Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 4 ...
- gulp 运用 的理解
ugulp.task('build', function() { runSequence('clean', 'copy', ['uglify', 'sass', 'htmlmin'], 'base64 ...
- centos6.5下redis安装步骤总结
1.首先下载一个版本 我用的是3.2.9 解压:tar -zxvf /redis-stable.tar.gz 在/usr/local/新建redis文件夹 然后把解压好的文件夹移动到/usr/loca ...
- Tomcat7安装(linux环境)
1.获取安装包 如果没有tomcat,则创建之,并下载二进制文件到该目录,如下: mkdir /opt/tomcat cd /opt/tomcat wget http://mirrors.hust.e ...