poj 3259 Wormholes spfa算法
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25582 | Accepted: 9186 |
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..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.
Input
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.
Output
Sample Input
- 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
Sample Output
- NO
- YES
Hint
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.
bellman-ford或者SPFA都能做,就是求负回路,bellman-ford的判断方法就是全图松弛 n - 1 次,如果还能继续松弛,说明有负圈回路,否则就说明没有负圈回路,bellman-ford有一个优化就是松弛的过程中如果在一次松弛过程中任何点都没有改变最短路径,那么就可以提前结束全图的松弛操作得到结果
spaf是bellman算法的一个优化,每次松弛的不是全图的点,而是与最短距离有变化的点相连的那些点,题目用到一个队列,每个点如果最短路径更新了,就扔到队列里,然后从队列中拿出点来松弛与其相连的点,如果一个点入队列的次数达到n此,说明图中有负圈
注意两个点之间可能有多条路径,选择权值较小的那条边留下
- #include<stdio.h>
- #include<string.h>
- #include<utility>
- #include<queue>
- using namespace std;
- int map[501][501];
- int dis[501];
- int n, m, w;
- int s, e, t;
- bool spfa()
- {
- bool flag[501] = {0};
- int count[501] = {0};
- queue<int > q;
- q.push(s);
- dis[s] = 0;
- int curr;
- int i;
- while(!q.empty())
- {
- curr = q.front();
- q.pop();
- for(i = 1; i <= n; i++)
- {
- if(map[curr][i] < 100000)
- {
- if(dis[i] > map[curr][i] + dis[curr] )
- {
- dis[i] = map[curr][i] + dis[curr];
- if(flag[i] == 0)
- q.push(i);
- count[i] ++ ;
- flag[i] = 1;
- if(count[i] >= n)
- return 0;
- }
- }
- }
- flag[curr] = 0;
- }
- return 1;
- }
- int main()
- {
- int f;
- scanf("%d", &f);
- while(f--)
- {
- memset(dis,63, sizeof(dis));
- memset(map, 127, sizeof(map));
- scanf("%d %d %d", &n, &m, &w);
- int i;
- for(i = 0; i < m; i++)
- {
- scanf("%d %d %d", &s, &e, &t);
- map[s][e] = map[s][e] > t? t : map[s][e];
- map[e][s] = map[e][s] > t? t : map[e][s];
- }
- for(i = 0; i < w; i++)
- {
- scanf("%d %d %d", &s, &e, &t);
- map[s][e] = -t;
- }
- if(spfa())
- printf("NO\n");
- else
- printf("YES\n");
- }
- return 0;
- }
poj 3259 Wormholes spfa算法的更多相关文章
- POJ 3259 Wormholes SPFA算法题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3259 Wormholes(SPFA)
http://poj.org/problem?id=3259 题意 : 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地, ...
随机推荐
- JS刷新页面的几种方法(转)
Javascript刷新页面的几种方法: 1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locat ...
- Android Studio如何添加override
而Android Studio如何添加呢?方法如下: 右键(或者Alt + Insert) --- Generate... ---- Override Method... 或者 Implement ...
- 数据库表转换成javaBean对象小工具
package test.utils; import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter; ...
- RSA加密工具包
主要参考: http://www.blogjava.net/icewee/archive/2012/05/19/378570.html http://snowolf.iteye.com/ 基于以上代码 ...
- IOS-归档的使用
归档又叫序列化(coding) // 归档NSKeyedArchiver // 解归档NSKeyedUnarchiver // 归档之后的文件 会被加密 // 可以归档的文件.数据--> ...
- ios外派—本公司长年提供ios程序员外派业务(北京动点软件,可签合同)
北京动点飞扬长年提供ios工程师外派业务. 我公司程序员平均技术情况如下: 1.二年以上iPhone/ipad开发经验:2.熟练使用Xcode.Objective C编码技能:3.熟悉iOS开发框架, ...
- asp.net 页面 输出之前修改 html(render)
protected override void Render(HtmlTextWriter writer) { StringWriter output = new StringWriter(); ba ...
- "XX cannot be resolved to a type "eclipse报错及解决说明
转自:http://zhaoningbo.iteye.com/blog/1137215 引言: eclipse新导入的项目经常可以看到“XX cannot be resolved to a type” ...
- Android:单元测试Junit的配置
在实际开发中,开发android软件的过程需要不断地进行测试.而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性.... ...
- [复变函数]第11堂课 3.3 Cauchy 积分定理及其推论
0. 引言 (1) Cauchy 积分定理: 设 $D$ 为 $(n+1)$ 连通区域, $f$ 在 $D$ 内解析且连续到边界 $C$, 则 $\dps{\int_C f(\zeta)\rd \ze ...