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 ...
随机推荐
- EasyExcel 解析excel
参考:https://blog.csdn.net/jiangjiandecsd/article/details/81115622 https://blog.csdn.net/jianggujin/ar ...
- Linux 用户、权限
用户:uid 保存在 /etc/passwd 用户分类: 管理员 uid--0 普通用户 --系统用户 uid 1-499 --一般用户 uid 500-60000 组:gid 保存在/etc/ ...
- ubuntu16.04搭建geodjango+postgresql+postgis的WebGIS框架(三)加载空间数据
之前两篇基本上搭好了geodjango开发的环境了,当然你的电脑上肯定要有python和django的环境(这个我就不介绍了,网上一搜一大堆),我自己用的python3.5和django2.0(毕竟2 ...
- Event 对象的属性和方法
事件触发时,会将一个 Event 对象传递给事件处理程序,比如: document.getElementById("testText").addEventListener(&quo ...
- Block 语法
Block,代码块,^符号是block的语法标记. 比如说,一个block的参数列表是一个UIView,返回值是个CGFloat,block名称是testBlock 可以定义为 CGFloat (^ ...
- sql中优化查询
1.在大部分情况下,where条件语句中包含or.not,SQL将不使用索引:可以用in代替or,用比较运算符!=代替not. 2.在没有必要显示不重复运行时,不使用distinct关键字,避免增加处 ...
- [ 转载 ] ssh连接远程主机执行脚本的环境变量问题
近日在使用ssh命令ssh user@remote ~/myscript.sh登陆到远程机器remote上执行脚本时,遇到一个奇怪的问题: ~/myscript.sh: line n: app: co ...
- JSTL的比较运算符有哪些,用例说说它们的作用
el表达式对应的运算符 等于 == eq 不等于 != ne 大于 > gt 小于 < lt 大于等于 >= ge 小于等于 <= ...
- sqlalchemy 学习-- 多表操作
一对多:一对一 # one -- many class Students(Base): __tablename__ = "students" sid = Column(Intege ...
- 生产者消费者 wait()。 notify()
public class ThreadDemo3 { public static void main(String[] args){ MyList list = new MyList(); Produ ...