POJ 3259 Wormholes(Bellman-Ford)
题目网址:http://poj.org/problem?id=3259
题目:
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 52198 | Accepted: 19426 |
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
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 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.
我们根本不需要关心他所处的起点的具体位置,我们只需要判断是否有负权环即可,所以将所有的dist[i]都初始化为无穷大。有负权环的话就输出YES,没有的话就输出NO。很自然地就会想到Bellman-Ford算法。判断第n次循环,是否还会松弛,如果还需要就说明有负权环。 这道题需要注意的一点是:虫洞是单向边,路径是双向边。
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = ;
struct node{
int v,u,w;
};
vector<node>v;
int n,m,w;
int dist[];
node x;
bool relax(int j){//松弛操作
if(dist[v[j].u]>dist[v[j].v]+v[j].w){
dist[v[j].u]=dist[v[j].v]+v[j].w;
return true;
}
return false;
}
bool bellman_ford(){
for (int i=; i<=n; i++) {
dist[i]=inf;
}
for (int i=; i<n-; i++) {
int flag=;
for (int j=; j<v.size(); j++) {
if(relax(j)) flag=;
}
if(!flag) return false;
}
for (int j=; j<v.size(); j++) {//核心
if(relax(j)) return true;
}
return false;
}
int main(){
int t;
cin>>t;
while (t--) {
int ok=;
v.clear();
cin>>n>>m>>w;
for (int i=; i<m; i++) {
cin>>x.v>>x.u>>x.w;
v.push_back(x);
swap(x.v, x.u);
v.push_back(x);
}
for (int i=; i<w; i++) {
cin>>x.v>>x.u>>x.w;
x.w=-x.w;
v.push_back(x);
}
if (bellman_ford()) printf("YES\n");
else printf("NO\n");
}
return ;
}
POJ 3259 Wormholes(Bellman-Ford)的更多相关文章
- POJ 3259 Wormholes Bellman题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- poj 3259 Wormholes
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
随机推荐
- 删除mac开机启动项
1、开“系统偏好设置”窗口,选择“用户与群组”,进入用户与群组窗口.选择登录项选项卡,再解锁,最后删除开机启动的应用. 2、分别在以下6个目录中检查是否有与anydesk相关的plist文件 ~/ ...
- 细谈Redis五大数据类型
文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 上一篇文章有提到,Redis中使用最频繁的有5种数据类型:String.List.Hash.Set.SortS ...
- Python2与Python3的map()
1. map()函数 Python2中,map(func, seq1[,seq2[...[,seqn)将func作用于seq*的每个序列的索引相同的元素,并最终生成一个[func(seq1[0], s ...
- eclipse与hadoop集成,运行wordCount1
搭好了hadoop集群之后,就该使用它了 第一步:下载hadoop eclipse的插件,将它放到eclipse\plugins的目录下,然后重启eclipse,点击windows->show ...
- Angular 文件上传、下载
1. 文件上传 本地可同时选择多个文件 将本地所选择的文件列出来 单个文件上传至服务器: 删除本地选择的文件 样式使用了bootstrap的样式 1. html - file.component.ht ...
- <机器学习>无监督学习算法总结
本文仅对常见的无监督学习算法进行了简单讲述,其他的如自动编码器,受限玻尔兹曼机用于无监督学习,神经网络用于无监督学习等未包括.同时虽然整体上分为了聚类和降维两大类,但实际上这两类并非完全正交,很多地方 ...
- WordPress新用户注册时提示“您的密码重设链接无效”
在使用Wordpress密码找回功能及新用户注册邮件中的重置密码链接时,Wordpress提示“您的密码重设链接无效,请在下方请求新链接.”.“该key似乎无效”.“invalid key”. 这个其 ...
- Webstorm轻松部署项目至服务器
wo大前端在开发环境下,需要将项目部署到测试环境,webstorm进行基础配置操作就可实现. 一.在Deployment选项下配置远程服务器地址 点击加号,选择type类型,Name自己填,帮你找到这 ...
- SpringBoot 连接kafka ssl 报 CertificateException: No subject alternative names present 异常解决
当使用较新版本SpringBoot时,对应的 kafka-client 版本也比较新,如果使用了 2.x 以上的 kafka-client ,并且配置了 kafka ssl 连接方式时,可能会报如下异 ...
- 最清晰的RESTFUL理解
Restful理解 API(Application Programming Interface),顾名思义:是一组编程接口规范,客户端与服务端通过请求响应进行数据通信.REST(Representat ...