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次松弛时,还能 ...
随机推荐
- SQL Stored Procedure and Function
Anything can be programmable with defined syntax and common lib. )) -- Add the parameters for the st ...
- Android - 页面返回上一页面的三种方式
今年刚刚跳槽到了新公司,也开始转型做Android,由此开始Android的学习历程. 最近在解很多UI的bug,在解bug过程中,总结了在UI的实现过程中,页面返回上一页面的几种实现方式. 一. 自 ...
- 二种方法安装卸载Windows服务的命令
第一种方法:通过Dos命令安装系统服务1. 开始 运行输入 cmd 进入dos窗口2. cd命令进入到C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727目录下, ...
- Oracle三种循环例题:打印九九乘法表
数据库SQL三种循环语句(For.While.Loop) --如果要将执行结果输出,需要先执行 setserveroutput on 命令,在窗口里显示服务器输出信息 set serveroutput ...
- kubernetes组件helm
1.安装helm Helm由客户端helm命令行工具和服务端tiller组成,Helm的安装十分简单. 下载helm命令行工具到master节点node1的/usr/local/bin下(只需要在其中 ...
- 概念:RPG游戏中两个兵种互相攻击的逻辑
直接上题目: 解析题目: 根据题目的解析,进行代码的实现: 输出结果: 心得: (1) 当我们面对‘公式结果不是我们想要的’时,应该在脑海里将一个完整的攻击流程进行想象,就会对流程有个更清晰的思路 ( ...
- 电路设计软件 电路模拟软件 sPlan , LTspice 等
电路设计/PCB绘制 立创EDA https://lceda.cn/ sPlan http://www.electronic-software-shop.com/splan-70.html?langu ...
- STM32开发-MDK新建工程及配置
本人也是接触stm32没多久,之前用的MDK是5.1,现在用的是5.13,MDK5.0之前的版本(本人简称旧版)和之后的版本(本人简称新版)新建工程有很大区别.对于刚开始用学stm32的新手来说,基本 ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) D】欧拉回路(分类讨论)
点此看题面 大致题意: 有一个\(n\)行\(m\)列的网格图,让你给每一条边设置一个通过次数(\(\ge1\)),使其成为欧拉回路,且通过次数总和最小. 初始化 首先,由于通过次数\(\ge1\), ...
- React的安装
创建: 2019/05/01 完成: 2019/05/01 create-react-app 学习及创建单页app npx create-react-app my-app cd my-app npm ...