题目链接: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. mousemove([[data],fn])

    mousemove([[data],fn]) 概述 当鼠标指针在指定的元素中移动时,就会发生 mousemove 事件.大理石构件来图加工 mousemove事件处理函数会被传递一个变量——事件对象, ...

  2. Material Icons 查找的替代办法

    1 问题 1.1 国内访问 google 困难,众所周知 1.2 在平时的工作中使用 Material Icons 如何查询呢 2 办法 2.1 github.com 把包 download 2.2 ...

  3. MySQL组提交(group commit)

    MySQL组提交(group commit) 前提: 以下讨论的前提 是设置MySQL的crash safe相关参数为双1: sync_binlog=1 innodb_flush_log_at_trx ...

  4. Shell 04 字符串处理、正则表达式

    一.字符串的处理   1.字符串截取 1.1 s{}表达式  ${变量名:起始位置:长度} (从0开始) n=number (n="number") echo ${#n}   -- ...

  5. 2018 Nowcoder Multi-University Training Contest 1

    Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdo ...

  6. Bzoj 1588: [HNOI2002]营业额统计(splay)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Description 营业额统计 Tiger最近被公司升任为营业部经理,他上 ...

  7. 「51Nod 1601」完全图的最小生成树计数 「Trie」

    题意 给定\(n\)个带权点,第\(i\)个点的权值为\(w_i\),任意两点间都有边,边权为两端点权的异或值,求最小生成树边权和,以及方案数\(\bmod 10^9 + 7\) \(n \leq 1 ...

  8. luogu2885

    P2885 [USACO07NOV]电话线Telephone Wire 给出若干棵树的高度,你可以进行一种操作:把某棵树增高h,花费为h*h. 操作完成后连线,两棵树间花费为高度差*定值c. 求两种花 ...

  9. PHP 之实现按日期进行分组、分页

    一.效果图 二.原始数据 array(6) { [0]=> array(8) { ["id"]=> string(1) "6" ["use ...

  10. 第11组 Beta冲刺(1/5)

    第11组 Beta冲刺(1/5)   队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/11913626.html 作业博客 https://www. ...