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. const和readonly的区别

    http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html http://www.codeproject.com/Tips/803 ...

  2. gulp browser-sync自动刷新插件

    很久没弄gulp了,都快忘了,今天又来温习下browser-sync 自动刷新插件,在安装的时候出现以下提示: $ npm install browser-sync --save-dev> ws ...

  3. 利用Java自带的MD5加密java.security.MessageDigest;

    MD5加密算法,即"Message-Digest Algorithm 5(信息-摘要算法)",它由MD2.MD3.MD4发展而来的一种单向函数算法(也就是HASH算法),它是国际著 ...

  4. fil_system_struct

    /** The tablespace memory cache */ typedef struct fil_system_struct fil_system_t; /** The tablespace ...

  5. jquery live hover绑定方法

    $(".select_item span").live({ mouseenter: function() { $(this).addClass("hover") ...

  6. BZOJ 3573 米特运输

    语文题... 原来除了hash还可以取对数啊orz #include<iostream> #include<cstdio> #include<cstring> #i ...

  7. Nginx & AWStats 安装、配置、使用

    —— 参考IBM文章:THIS , 不一样的指导顺序 —— 1. awstats分析nginx - access.log,网上资料大部分都是下载,然后配置.官网下载地址: http://awstats ...

  8. POI读取Word与Excel

    import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; ...

  9. vc/mfc获取rgb图像数据后动态显示及保存图片的方法

    vc/mfc获取rgb图像数据后动态显示及保存图片的方法 该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw3 ...

  10. 转载RabbitMQ入门(1)--介绍

    目录[-] "Hello World" (使用java客户端) 发送 接收 把所有放在一起 前面声明本文都是RabbitMQ的官方指南翻译过来的,由于本人水平有限难免有翻译不当的地 ...