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块地, ...
随机推荐
- html初学(二)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <table bor ...
- 你了解System.out.println()的真正含义吗?
在Java编程中,我们常常用 System.out.println(); 来输出字符串,也许我们都已经猜到println()是方法名,但System是什么,out又是什么呢? 其实System是jav ...
- Hive(五):hive与hbase整合
配置 hive 与 hbase 整合的目的是利用 HQL 语法实现对 hbase 数据库的增删改查操作,基本原理就是利用两者本身对外的API接口互相进行通信,两者通信主要是依靠hive_hbase-h ...
- [Hibernate] - Select/Update/Delete/Insert
Java bean: package com.my.bean; import java.util.Date; public class WorkPack { private String uWorkP ...
- LeetCode "Longest Substring with At Most K Distinct Characters"
A simple variation to "Longest Substring with At Most Two Distinct Characters". A typical ...
- Spring小结
一.环境搭建 创建Maven项目 一般pom.xml会出错,本地若无相应版本的jar包,则无法下载或下载速度非常慢,我的解决方案是,查找本地仓库的jar,修改为本地仓库有的jar即可 pom.xml的 ...
- 什么是SQLCLR与使用
原帖地址:http://www.cnblogs.com/hsrzyn/archive/2013/05/28/1976555.html 什么是SQLCLR SQL CLR (SQL Common Lan ...
- gcc中动态库和静态库的链接顺序
so文件:动态库a文件: 静态库exe文件:可执行程序(linux下以文件属性来标示是否是可执行文件,与后缀名无关) 经过自己写的一些测试程序,大致了解了下gcc中链接顺序问题,总结出以下几点:1,动 ...
- System.ArgumentOutOfRangeException: 年、月和日参数描述无法表示的 DateTime。
c#日期控件 格式设为 yyyy-MM,通过updown 方式调整日期. 当为月度最后一天,且要调整月没有当前月的最后一天时,就会报标题错误. 如:当前为1月31日,要调整为2月时,就会报错.因为2月 ...
- oracle学习笔记(四)oracle内存优化
emca -config dbcontrol db -repos recreate 解决'oracle Environment variable ORACLE_SID not defined. Ple ...