题目链接:http://poj.org/problem?id=3259

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions:75598   Accepted: 28136

Description

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..NM (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.

Input

Line 1: A single integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
题目大意:n 个点, m 条双向边,代表经过所需的时间,w 条单向边,代表经过所减少的时间。问该图中是否存在负环。
思路:
1.简单的判环,用spfa即可,在不存在环的情况下,任意点在进行路径松弛时,最多被其他的点更新一次。那么任意点的最多入队次数只能是 n 次。当存在任何一点的入队次数大于顶点数n,即说明存在环。
2.判负环,即路径往小松弛。判正环时,即路径往大更新。注意dis数组初始化即可。
代码如下:
 #include<stdio.h>
#include<queue>
#include<string.h>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
const int MAXM = ;
const int inf = 0x3f3f3f3f;
using namespace std; int n, m, k; //n个点 m条双向边 k个虫洞(单向边)
int head[MAXN], cnt;
int vis[MAXN], num[MAXN];//num表示第i个点的入队次数 用来判断负环是否存在
int dis[MAXN], flag;
queue<int> Q; struct Edge
{
int to, next, w;
}edge[ * MAXM]; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].w = c;
edge[cnt].next = head[a];
head[a] = cnt;
} void spfa(int st)
{
while(!Q.empty()) Q.pop();
mem(vis, ), mem(dis, inf), mem(num, );
Q.push(st);
vis[st] = ;
num[st] = ;
dis[st] = ;
while(!Q.empty())
{
int a = Q.front();
Q.pop();
vis[a] = ;
for(int i = head[a]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dis[to] > dis[a] + edge[i].w)
{
dis[to] = dis[a] + edge[i].w;
if(!vis[to])
{
vis[to] = ;
Q.push(to);
num[to] ++;
if(num[to] > n)
{
flag = ;
break;
}
}
}
}
if(flag)
break;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
cnt = , flag = , mem(head, -);
scanf("%d%d%d", &n, &m, &k);
for(int i = ; i <= m; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
}
for(int i = ; i <= k; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, -c);
}
spfa();
}
return ;
}

POJ3259

 

POJ3259 Wormholes 【spfa判负环】的更多相关文章

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

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

  2. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  3. POJ3259 Wormholes(SPFA判断负环)

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

  4. POJ 3259 Wormholes(SPFA判负环)

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

  5. poj3259(spfa判负环)

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

  6. POJ3259 Wormholes —— spfa求负环

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

  7. Poj 3259 Wormholes(spfa判负环)

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

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

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

  9. spfa判负环

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

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

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

随机推荐

  1. 第四章 深入C#的string类

    一.String 类的常用方法 1.indexOf(); 获取指定字符串的位置,如果没有则返回-1 2.SubString();    截取字符串,参数1代表开始位置,参数2代表截取长度 3.ToLo ...

  2. js校验密码必须包含字母大小写、数字

    校验密码必须包含字母大小写.数字 function checkPasswordNew(s){ var str=trim(s); //var reg = /^(?![A-Z]+$)(?![a-z]+$) ...

  3. MySQL 热快问题解决

    原文地址:http://blog.itpub.net/22664653/viewspace-1269948 一  背景 某个业务线 商品开放开用户申请免费试用,当某个商品特别吸引人时,比如iPhone ...

  4. 百度ueditor中复制word图文时图片转存任然保持灰色不可用

    官网地址http://ueditor.baidu.com Git 地址 https://github.com/fex-team/ueditor 参考博客地址 http://blog.ncmem.com ...

  5. ckeditor粘贴word图片自动上传功能

    由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...

  6. XML、HTML、XHTML的关系

    标记语言 XML.HTML.XHTML这三者都有ML.ML(Markup Language)标记语言在维基百科中的解释是: 一种将文本以及文本相关的信息结合起来,展示出关于文档结构和数据处理细节的计算 ...

  7. CodeChef October Lunchtime 2019 Division 2

    HIT: Khaled in HIT 题目描述 Khaled 教练是 HIT(Hag Institute of Technology)一位名师.但是,他有一些困扰. 最近,Khaled 教练正在教一门 ...

  8. 【线性代数】7-2:线性变化的矩阵(The Matrix of a Linear Transformation)

    title: [线性代数]7-2:线性变化的矩阵(The Matrix of a Linear Transformation) categories: Mathematic Linear Algebr ...

  9. 【CTS2019】珍珠【生成函数,二项式反演】

    题目链接:洛谷 pb大佬说这是sb题感觉好像有点过fan...(我还是太弱了) 首先,设$i$这个数在序列中出现$a_i$次,要求$\sum_{i=1}^D[a_i \ mod \ 2]\leq n- ...

  10. tesseract 安装及使用

    安装软件 tesseract下载地址:https://digi.bib.uni-mannheim.de/tesseract/ 安装即可! 安装完成tesseract-ocr后,需要做一下配置 . 在P ...