POJ3259 :Wormholes

时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u
描述

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: 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.
输出
Lines 1..F: For each farm, output "YES" if FJ
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 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.
 
题意:

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判负环)的更多相关文章

  1. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  2. POJ3259 Wormholes(SPFA判断负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  3. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  4. poj3259(spfa判负环)

    题目连接:http://poj.org/problem?id=3259 题意:John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时 ...

  5. POJ3259 Wormholes —— spfa求负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  6. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  7. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  8. spfa判负环

    bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...

  9. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

随机推荐

  1. Mysql IN语句查询

    语法: WHERE column IN (value1,value2,...) WHERE column NOT IN (value1,value2,...) 1.in 后面是记录集,如: selec ...

  2. 如何在notepad++实现代码自动化排版(调用Astyle)

    我先介绍这个怎么在notepad++中调用原版的astyle的方法. 在notepad++:运行或是F5, 在输入框中选择astyle.exe所在的目录,什么你没有astyle,下载地址https:/ ...

  3. 机器学习进阶-案例实战-图像全景拼接-图像全景拼接(RANSCA) 1.sift.detectAndComputer(获得sift图像关键点) 2.cv2.findHomography(计算单应性矩阵H) 3.cv2.warpPerspective(获得单应性变化后的图像) 4.cv2.line(对关键点位置进行连线画图)

    1. sift.detectAndComputer(gray, None)  # 计算出图像的关键点和sift特征向量 参数说明:gray表示输入的图片 2.cv2.findHomography(kp ...

  4. day30-模块和包

    一.模块介绍 1.什么是模块 在python中,一个函数封装一个功能,当一个文件中包含很多个函数,而我们在其他程序中经常会用到这个文件中的功能时,那么我们就可以将这个包含多个函数的文件封装成一个模块, ...

  5. Spring boot 注册Filter , Listener, Servlet

    1: ServletRegistrationBean   Servlet @Bean public ServletRegistrationBean myServlet(){ ServletRegist ...

  6. 爬虫--requests模块学习

    requests模块 - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能 ...

  7. Netty - 1

    Netty设计特点: 1. io线程模型 使用reactor模式,同步非阻塞.这决定了可以用最少的资源做更多的事. 2. 内存零拷贝 使用直接缓存 3. 内存池设计 申请的内存可以重用,主要指直接内存 ...

  8. 调用DATASNAP+FIREDAC的远程方法有时会执行二次SQL或存储过程的BUG(转永喃兄)

    调用DATASNAP+FIREDAC的远程方法有时会执行二次SQL或存储过程的BUG 1)查询会重复执行的情形:Result := DATASETPROVIDER.Data会触发它关联的DATASET ...

  9. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  10. MYSQL数据库中,常见的数据类型有哪些?它们与java中的数据类型如何对应

    A.常规 映射 integer 或者 int int 或者 java.lang.Integer INTEGER 4 字节 long long Long BIGINT 8 字节 short short  ...