点击打开链接

Wormholes
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..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..M+W+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.

题目大意是给你一些虫洞和路径,通过虫洞可以回到过去,题目问你是否经过这些虫洞可以回到最初开始的地方,并且时间还要是before

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算法的更多相关文章

  1. POJ 3259 Wormholes SPFA算法题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  2. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  3. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  4. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  5. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  6. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  7. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  8. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  9. POJ 3259 Wormholes(SPFA)

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

随机推荐

  1. 通过CSS禁用页面模块的复制和粘贴功能

    样式代码: -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; -khtml-user-select: ...

  2. hibernate 自生双向一对多 多对一管理 (树)

    <span style="font-size: large;">package com.javacrazyer.test; import java.io.Seriali ...

  3. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  4. java解析出url请求的路径和参数键值对类 - 转

    import java.util.HashMap; import java.util.Map; public class CRequest { /** * 解析出url请求的路径,包括页面 * @pa ...

  5. apache 2.4 配置多个站点

    1.打开\Apache24\conf\httpd.conf 查找conf/extra/httpd-vhosts.conf  去掉前面的#号,一般是去掉的 2.在httpd.conf 中查找Requir ...

  6. 【故障处理】ORA-12162: TNS:net service name is incorrectly specified

    本文将给大家阐述一个因未设置系统环境变量ORACLE_SID导致ORA-12162错误的案例.希望大家有所思考. 1.获得有关ORA-12162报错信息的通用表述信息[oracle@asdlabdb0 ...

  7. noaman日志第一条:2015-1024;“Hello.World”

    在南京的不知道第几个周末,一夜的煎熬终于活过来了.清早起来开通了自己的博客,第一条说说就记录开通博客这个事件.没有别的. 之后我会着重记录每天看书内容,以及所要编写的重要程序,一点一滴地积累希望能收获 ...

  8. 成功移植SQLite3到ARM Linux开发板

    SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...

  9. 通过共享用户ID来实现多个应用程序使用同一个进程(一些情况的测试)

    从很多方面来看,每个Android 应用程序都存在于它自己的世界之中:• 默认情况下,每个应用程序均运行于它自己的Linux 进程中.当应用程序中的任意代码开始执行时,Android 启动一个进程,而 ...

  10. HashMap的实现

    HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如此 ...