POJ:3259-Wormholes(最短路判断负环)
Wormholes
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 58153 Accepted: 21747
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: 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.
解题心得:
- 题意就是一个图里面有很多双向边,也有单向的负权路径,问有没有负权环。
- 这里主要就是问了一个关于最短路中怎么判断是否存在负环,一般有负权路径的图都是用SPFA来跑,如果不存在负环一个点最多被压入队列中n次(被其余的每个点更新),那么如果大于n次说明存在负环,还是很容易想明白的,代码实现也很简单,记录一下压入队列的次数就行了。
#include <stdio.h>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 510;
int Time[maxn],n,m,w;
bool vis[maxn];
int maps[maxn][maxn];
void init() {
memset(Time,0x3f, sizeof(Time));
memset(maps,0x3f,sizeof(maps));
scanf("%d%d%d",&n,&m,&w);
for(int i=0;i<m;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(maps[a][b] > c)
maps[a][b] = maps[b][a] = c;
}
for(int i=0;i<w;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
c = -c;
if(maps[a][b] > c)
maps[a][b] = c;
}
}
bool spfa() {
int num[maxn];
memset(num,0, sizeof(num));
memset(vis,0,sizeof(vis));
num[1] = 1;
queue <int> qu;
qu.push(1);
Time[1] = 0;
while(!qu.empty()) {
int now = qu.front(); qu.pop();
vis[now] = false;
for(int i=1;i<=n;i++) {
if(maps[now][i] != 0x3f3f3f3f) {
if(Time[now] + maps[now][i] < Time[i]) {
Time[i] = maps[now][i] + Time[now];
if(!vis[i]) {
vis[i] = true;
qu.push(i);
num[i]++;
if(num[i] > n) {
return true;
}
}
}
}
}
}
return false;
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
init();
bool is_back = spfa();
if(is_back)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
POJ:3259-Wormholes(最短路判断负环)的更多相关文章
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- poj 3259 Wormholes(bellman-ford判断负环)
题目链接:http://poj.org/problem?id=3259 题目就是问你能否回到原点而且时间还倒回去了.题目中有些路中有单向的虫洞能让时间回到过去 所以只要将虫洞这条边的权值赋为负然后再判 ...
- poj 3259 Wormholes【spfa判断负环】
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36729 Accepted: 13444 Descr ...
- (简单) POJ 3259 Wormholes,SPFA判断负环。
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes【Bellman_ford判断负环】
题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说 ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- POJ 3259 Wormholes 最短路+负环
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...
- Poj(3259),SPFA,判负环
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- uva 558 - Wormholes(Bellman Ford判断负环)
题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...
随机推荐
- (开发)bable - es6转码
参考:http://www.ruanyifeng.com/blog/2016/01/babel.html Babel Babel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执 ...
- js实现弹幕效果
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...
- 《ArcGIS Runtime SDK for Android开发笔记》——(1)、Android Studio下载与安装
1.前言 Android Studio 是一个Android开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工 ...
- SharePoint 和 Windows Phone 7 开发人员培训资源
该工具包包括单位数目: Windows Phone 7 发展简介 在本单元中,您将了解真实的消费信息存储在 SharePoint 中与你用 Visual Studio 写 Windows Phone ...
- ES6相关特性(let & const)
[ecma-262/8.0]http://www.ecma-international.org/ecma-262/8.0/index.html 1.Let & const let 的三个特性: ...
- simotion byte/word ASCII码转换为字符、字符串
建立string类型,和byte类型(ASCII)的数据 将byte类型(ASCII)赋值给string中的一个字符 参考程序 VAR_GLOBAL myword :WORD; mystring :S ...
- Makedown语法说明
Markdown 语法说明 (简体中文版) / (点击查看快速入门) 概述 宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 链接 强调 ...
- 运行npm update等命令出错后如何分析问题根源
我今天工作时,在当前前端项目工作目录下执行命令npm update 结果遇到如下错误:registry error parsing json npm ERR! Unexpected token < ...
- Buffer实例
互联网的基础是数据的传送,一切都围绕着数据展开,比如发送啊,接收啊,这一切都离不开网络,通过之前,学会了通过http模块来搭建一个服务器,也实现了网络爬虫,nodejs中网络的部分,Net这个模块,对 ...
- python 删除空白
Python能够找出字符串开头和末尾多余的空白.要确保字符串末尾没有空白,可使用方法rstrip() . >>> favorite_language = 'python ' > ...