POJ Wormholes (SPFA)
http://poj.org/problem?id=3259
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.
SPFA判断是否有负权,如果一个点进入队列的次数达到总点数则说明有负权
dist[i]数组记录源点到i的最短路径,与Dijsktar不同的是dist[i]多次更新
use[i]记录i点进入队列的次数,即dist[i]被更新的次数;
vis[i]标记i点是否进入队列
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<queue>
#define INF 0xffffff
#define N 520
using namespace std; struct node
{
int e, w;
}; vector<node>G[N];
int n, use[N], dist[N];
bool vis[N]; void Init()
{
int i;
memset(vis, false, sizeof(vis));
memset(use, , sizeof(use));
for(i = ; i <= n ; i++)
{
G[i].clear();
dist[i] = INF;
}
} int SPFA(int s)
{
queue<node>Q;
node now, next;
int i, len;
now.e = s;
now.w = ;
dist[s] = ;
Q.push(now);
vis[s] = true;
use[now.e]++;
while(!Q.empty())
{
now = Q.front();
Q.pop();
vis[now.e] = false; len = G[now.e].size();
for(i = ; i < len ; i++)
{
next = G[now.e][i];
if(dist[next.e] > dist[now.e] + next.w)
{
dist[next.e] = dist[now.e] + next.w;
use[next.e]++;
if(use[next.e] >= n)
return ;
if(!vis[next.e])
{
vis[next.e] = true;
Q.push(next);
}
}
}
}
return ;
} int main()
{
int T, m, w, s, e, t, i;
node p;
scanf("%d", &T);
while(T--)
{ scanf("%d%d%d", &n, &m, &w);
Init();
for(i = ; i <= m ; i++)
{
scanf("%d%d%d", &s, &e, &t);
p.w = t;
p.e = s;
G[e].push_back(p);
p.e = e;
G[s].push_back(p);
}
for(i = ; i <= w ; i++)
{
scanf("%d%d%d", &s, &e, &t);
p.w = -t;
p.e = e;
G[s].push_back(p);
}
if(SPFA())
printf("YES\n");
else
printf("NO\n");
}
return ;
}
POJ Wormholes (SPFA)的更多相关文章
- POJ 3259 Wormholes(SPFA)
http://poj.org/problem?id=3259 题意 : 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地, ...
- POJ 1860(spfa)
http://poj.org/problem?id=1860 题意:汇率转换,与之前的2240有点类似,不同的是那个题它去换钱的时候,是不需要手续费的,这个题是需要手续费的,这是个很大的不同. 思路: ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- POJ题目(转)
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法: (1)枚举. (poj1753,poj29 ...
- 最短路(SPFA)
SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...
- Bellman-Ford算法及其队列优化(SPFA)
一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...
- Repeater POJ - 3768 (分形)
Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...
- Booksort POJ - 3460 (IDA*)
Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...
随机推荐
- Android开发之“点9”
“点九”是andriod平台的应用软件开发里的一种特殊的图片形式,文件扩展名为:.9.png智能手机中有自动横屏的功能,同一幅界面会在随着手机(或平板电脑)中的方向传感器的参数不同而改变显示的方向,在 ...
- [POJ1753]Flip Game(开关问题,枚举)
题目链接:http://poj.org/problem?id=1753 和上一个题一样,将初始状态存成01矩阵,就可以用位运算优化了.黑色白色各来一遍 /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏ ...
- UVa 11752 (素数筛选 快速幂) The Super Powers
首先有个关键性的结论就是一个数的合数幂就是超级幂. 最小的合数是4,所以枚举底数的上限是pow(2^64, 1/4) = 2^16 = 65536 对于底数base,指数的上限就是ceil(64*lo ...
- UVa 11400 Lighting System Design
题意: 一共有n种灯泡,不同种类的灯泡必须用不同种电源,但同一种灯泡可以用同一种电源.每种灯泡有四个参数: 电压值V.电源费用K.每个灯泡的费用C.所需该种灯泡的数量L 为了省钱,可以用电压高的灯泡来 ...
- TYVJ 1066 合并果子【优先队列】
题意:给出n堆果子,需要将n堆果子合并成一堆,问将所有堆的果子合成一堆所需要花费的最少的力气 因为要使耗费力气最小,即需要每次搬动的那堆重量小,所以可以选取两堆最轻的合并,合并之后再插入还没有合并的堆 ...
- IIS Web负载均衡的几种方式
Web负载均衡的几种实现方式 摘要:负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用 ...
- [swustoj 373] Antiprime数
Antiprime数(0373) 问题描述 如果一个自然数n(n>=1),满足所有小于n的自然数(>=1)的约数个数都小于n的约数个数,则n是一个Antiprime数.譬如:1, 2, 4 ...
- Share SDK分享
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os. ...
- 运维角度浅谈MySQL数据库优化(转)
一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善.这篇博文主要谈MySQL数据库发展周期中所面临的问题及优化方案,暂且抛开前端应用不说,大致分 ...
- 【 D3.js 高级系列 — 10.0 】 思维导图
思维导图的节点具有层级关系和隶属关系,很像枝叶从树干伸展开来的形状.在前面讲解布局的时候,提到有五个布局是由层级布局扩展来的,其中的树状图(tree layout)和集群图(cluster layou ...