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. leetcode509

    public class Solution { public int Fib(int N) { ) { ; } ) { ; } else { List<int> list = new Li ...

  2. 一次ssh远程不能登录的排查

    原创文件,欢迎阅读,禁止转载. 今天发现一台主机不能远程了,ssh连接不上了. 排查过程是这样的:1. ping没问题. 2. 通过telnet看端口是否开启.[user@localhost ~]$ ...

  3. sql 日期格式

    select CONVERT(varchar(10), getDate(),121) --不要时间2002-01-01 select CONVERT(varchar(10), getDate(),12 ...

  4. dev16 cxgrid 在DLL里报0地址错

    dev16 cxgrid 在DLL里Form里使用,报0地址错,在EXE里正常.c++builder 的DLL报错,delphi也报错. First chance exception at $09CE ...

  5. zipfile模块

    在python中操作zip文件, 基本上都是使用zipfile模块,他可以创建.解压文件,获取zip文件的元数据信息. 我们想要操作一个zip文件,第一步就是初始化ZipFile实例. 1.打开tes ...

  6. native.js 判断是否安装某app

    例:是否安装微信 function isWeixin() { var UIApplication = plus.ios.importClass("UIApplication"); ...

  7. 利用目录函数(opendir,readdir,closedir)查找文件个数

    如何知道一个目录下的所有文件个数呢?或许可以用tree来学(zhuang)习(bi)的同时知道文件个数.Linux系统io函数为我们提供了目录操作函数,其中有一个比较重要(实际上有三个,因为它们经常配 ...

  8. suse 关于使用 /etc/init.d/boot.local的问题

    最近看了一个问题,有同事在 suse环境下的/etc/init.d/boot.local 中,增加了一行脚本. 该脚本的简单大意如下: #!/bin/bash ] do ] then echo &qu ...

  9. 转:用JS获取地址栏参数的方法(超级简单)

    转载链接: http://www.cnblogs.com/fishtreeyu/archive/2011/02/27/1966178.html 方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实 ...

  10. shell字符串基本操作

    shell脚本中一切变量皆字符串,所以必须掌握字符串的常用处理方法.比如获取字符串长度.获取字符串指定位置字符.替换字符串中的指定字符或者删除某些字符等操作. 1.字符串操作列表 (1)var=val ...