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..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.. MW+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.

Output

Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

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 1, FJ cannot travel back in time. 
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)的更多相关文章

  1. POJ 3259 Wormholes(SPFA)

    http://poj.org/problem?id=3259 题意 : 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地, ...

  2. POJ 1860(spfa)

    http://poj.org/problem?id=1860 题意:汇率转换,与之前的2240有点类似,不同的是那个题它去换钱的时候,是不需要手续费的,这个题是需要手续费的,这是个很大的不同. 思路: ...

  3. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  4. Poj 3259 Wormholes(spfa判负环)

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

  5. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  6. 最短路(SPFA)

    SPFA是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 主要思想是: 初始时将起点加入队列.每次从队列中取出一个元素,并对所有与它相邻的点进行修改,若某个相邻的点修改成功,则将 ...

  7. Bellman-Ford算法及其队列优化(SPFA)

    一.算法概述 Bellman-Ford算法解决的是一般情况下的单源最短路径问题.所谓单源最短路径问题:给定一个图G=(V,E),我们希望找到从给定源结点s属于V到每个结点v属于V的最短路径.单源最短路 ...

  8. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  9. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

随机推荐

  1. C++11 - 类型推导auto关键字

    在C++11中,auto关键字被作为类型自动类型推导关键字 (1)基本用法 C++98:类型 变量名 = 初值;   int i = 10; C++11:auto 变量名 = 初值;  auto i ...

  2. javascript的模块化解读

    AMD是RequireJS在推广过程中对模块定义的规范化产出. 异步加载模块,依赖前置,提前执行. Define定义模块 define(['require','foo'],function(){ret ...

  3. WinForm特效:桌面上的遮罩层

    一个窗体特效,帮你了解几个windows api函数.效果:windows桌面上增加一个简单的遮罩层,其中WS_EX_TRANSPARENT 比较重要,它实现了鼠标穿透的功能. using Syste ...

  4. java虚拟机指令dup的理解

    举个例子: public class ExceptionTest{ void cantBeZero(int i) throws Exception{ throw new Exception(); } ...

  5. react-native项目中禁止截屏与录屏

    在android/app/src/main/java/com/projname/MainActivity.java文件中的onCreate方法添加一下代码即可 import android.view. ...

  6. Jquery动态添加/删除表格行和列

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. Java入门系列 Lambda表达式

    https://blog.csdn.net/bitcarmanlee/article/details/70195403

  8. Qt深入浅出(十五)QTableView

    转载:吓人的猿 TableView 表格视图控件QTableView,需要和QStandardItemModel, 配套使用,这套框架是基于MVC设计模式设计的,M(Model)是QStandardI ...

  9. delphi面向对象 继承窗体

    delphi继承form TFrmBase = class(TForm) procedure FormShow(Sender: TObject); end; procedure TFrmBase.Fo ...

  10. java线程中断的办法

    目录 中断线程相关的方法 中断线程 for循环标记退出 阻塞的退出线程 使用stop()方法停止线程 中断线程相关的方法 中断线程有一些相应的方法,这里列出来一下. 注意,如果是Thread.meth ...