POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes
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.
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.
can achieve his goal, otherwise output "NO" (do not include the
quotes).
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
NO
YES
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.
FJ的农场有很多虫洞,可以实现时光倒流,现在给你农场每条道路的起点、终点和时间,还有虫洞的起点、终点和能倒流的
时间,问FJ能不能通过时光倒流看到之前的自己。
首先T组数据,再输入n,m,w 分别代表n个节点,m个无向边的信息,w个有向虫洞的信息
思路:
把w个虫洞当负值加进边就行
比如第一组数据的3 1 3,在构图的时候add_edge(3,1,-3),即可。
其实就是用spfa跑一遍图,看一下有没有负环即可。有的话输出YES,没有的话输出NO
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std;
typedef long long LL;
#define Max_N 1001
#define INF 100000000
int head[Max_N],vis[Max_N],n,In[Max_N],cnt;
int dis[Max_N];
int pa[Max_N];
struct note{
int from,to,next;
int c;
}edge[*];
void init(){
memset(head,-,sizeof(head));
cnt = ;
}
void add_edge(int u,int v,double c){
edge[cnt].from = u;
edge[cnt].to = v;
edge[cnt].c = c;
edge[cnt].next = head[u];
head[u] = cnt++;
}
bool spfa(int s,int n,int key){
queue<int>q;
for(int i = ;i <= n ; i++)
dis[i] = INF;
memset(vis,,sizeof(vis));
memset(In,,sizeof(In));
q.push(s);
vis[s] = ;
dis[s] = ;
if(key == -)
memset(pa,-,sizeof(pa));
while(!q.empty()){
int p = q.front();
vis[p] = ;
q.pop();
for(int i = head[p] ; ~i ; i = edge[i].next){
if(key==i)continue;
int temp = edge[i].to;
if(dis[temp] > dis[p] + edge[i].c){
dis[temp] = dis[p] + edge[i].c;
if(key==-)
pa[temp] = i;
if(!vis[temp]){
q.push(temp);
vis[temp] = ;
if(++In[temp]>n)return false;
}
}
}
}
return true;
}//spfa带记录路径,通过Key调整
int main(){
int m,t,w;
for(scanf("%d",&t);t--;){
scanf("%d %d %d",&n,&m,&w);
init();
int x,y,c;
for(int i = ; i < m ; i++){
scanf("%d %d %d",&x,&y,&c);
add_edge(x,y,c);
add_edge(y,x,c);
}
for(int i = ; i < w ; i++){
scanf("%d %d %d",&x,&y,&c);
add_edge(x,y,-c);
}
spfa(,n,-)?puts("NO"):puts("YES");
}
return ;
}
POJ3259 :Wormholes(SPFA判负环)的更多相关文章
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- POJ3259 Wormholes(SPFA判断负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- poj3259(spfa判负环)
题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时 ...
- POJ3259 Wormholes —— spfa求负环
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- spfa判负环
bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
随机推荐
- 来分析一个UVC的摄像头的枚举信息
使用到工具USBlyzer导出数据,但是会发现一些还有部分解析未完全.我们将借助UVCView.x86(https://files.cnblogs.com/files/libra13179/77772 ...
- Notification html5 的通知api
https://developer.mozilla.org/zh-CN/docs/Web/API/notification 使用方法 var notification = new Notificati ...
- VSFTP 服务器配置
解决root用户无法登陆ftp传输文件的问题 配置vsftpd用户,启用root用户. #cd /etc/vsftpd #vi ftpusers 注释掉root 修改user_list文件 # ...
- Vue 修改dist 目录.
前后端分析之后,前端 打包处理 2
- redis如何清除所有的key
redis比memcache好的地方之一,如果memcache,恐怕就得关掉重启了. 1 使用cli FLUSHDB 清除一个数据库,FLUSHALL清除整个redis数据. 2 使用shell re ...
- python中Strip()函数的用法
Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列. 注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符. str.strip([chars]) ...
- SpringBoot配置发送邮件
一.导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- js 编写一个神奇的四则运算
写一个算法,有时候可以用简单的方法就可以写出来,但是只能针对特定的环境,如果要能够适应不同的环境,就需要对算法进行优化,在优化的过程中,你会觉得非常神奇,下面来看一个简单的四则运算的算法编写方式: 1 ...
- 使用git pull与本地文件冲突
出错信息如下: error: Your local changes to 'c/environ.c' would be overwritten by merge. Aborting. Please, ...
- vue启动时报错,node-modules下xxx缺失
从qq上拷贝了一个项目,解压后打开进vscode,安装依赖与scss后启动,显示node-modules下xxx指向缺失(想不起来是哪个缺失了),在网上找了很多解决办法,包括重新安装node 与 np ...