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 ...
 
随机推荐
- nginx 代理跨域
			
跨域 nginx设置http{ add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers X- ...
 - 腾讯云Linux VPS新硬盘分区与挂载教程(面板重装不丢失数据)
			
以腾讯云Centos系统服务器为例,小记的是数据盘不在本地,大小为20G,以下的教程来自小夕博客的一篇相关添加教程的修改,适合腾讯云Linux Centos系统.说明:参数也许不对,我没有截图了,但所 ...
 - ThreadLoacl 小记
			
参考地址: https://www.cnblogs.com/dolphin0520/p/3920407.html ThreadLoacl 本地线程变量 为线程创建一个副本, 一个内部类ThreadLo ...
 - jquery 属性-记住
			
$("").attr(); $("").removeAttr(); $("").prop(); $("").remove ...
 - jquery实现增删改(伪)-老男孩作业day13
			
使用jquery进行,文件的编写,实现自增id,删除,添加,编辑模式. jquery放在本地,src="jquery_js.js" 可以改成其他,或者在线的路径 readme &l ...
 - AS2 笔记 1——attachMovie 添加库影片
			
this["container"].attachMovie("useLoad", "useLoadMc", this.getNextHigh ...
 - Boolean类型
			
Boolean类型是与布尔值对应的引用类型.如果要创建Boolean对象,语法如下: var booleanObject = new Boolean(true); Boolean类型的实例重写了val ...
 - 页面中href链接的碰撞
			
问题: 如上图动态生成一个li列表,点击之后页面直接全部刷新了一遍 原因: 动态生成的代码(第一个图)中href链接地址与顶部页面(上图)中固定的href链接存在冲突 如果动态生成的列表指定的链接名称 ...
 - vue项目插入视频-mp4
			
1. v.vue文件: <template> <div> <div class="contain"> <my-video :sources ...
 - Java如何创建参数个数不限的函数
			
可变的参数类型,也称为不定参数类型.英文缩写是varargus,还原一下就是variable argument type.通过它的名字可以很直接地看出来,这个方法在接收参数的时候,个数是不定的. pu ...