点击打开链接

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. HTTP API 设计指南(中文版) restfull

    http://www.css88.com/archives/5121 目录 基础 总是使用TLS 在Accepts头中带上版本号 通过Etags支持缓存 用Request-Ids追踪请求 用Range ...

  2. mysql的text的类型注意

    不要以为text就只有一种类型! Text也分为四种类型:TINYTEXT.TEXT.MEDIUMTEXT和LONGTEXT 其中 TINYTEXT 256 bytes TEXT 65,535 byt ...

  3. The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine

    问题描述: 修改一个工具功能为读取excel文件中的数据(xls) 本机(windows server 2003 32位) 调试运行正常,部署到服务器(windows server 2003 64位) ...

  4. javascript 与和非

    || :  在javascript中,返回第一个真值,除非都是假值返回最后一个值(也是假值). 1 || 0; 0 || 1; 0 || 0; 0 || undefined; // undefined ...

  5. 10 程序员必备:Linux日常维护命令

    一.服务器硬件配置 1.查看硬盘及分区情况 # fdisk -l 2.查看分区空间使用情况 可以查看各分区大小.已使用.可用.已使用百分比.挂载情况 1)默认单位为K # df 2)可读性更好的显示, ...

  6. Redis启动警告错误解决

    启动错误 (1)WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxc ...

  7. MIME对应表

    文件后缀与MIME类型的对应表            'ai' => 'application/postscript',            'aif' => 'audio/x-aiff ...

  8. php缓存数组到文件

    php缓存数组到文件 static function getIDs($kemuid) { $cachefile="cache/" . $kemuid . ".cache& ...

  9. 打开SDK Manager检查Android SDK下载和更新失败的解决方法

    [故障描述] 打开SDK Manager检查Android  SDK状况,出现以下情况: Failed to fetch URL https://dl-ssl.google.com/android/r ...

  10. oracle行转列(连接字符串函数)

    方法一:wmsys.wm_concat(column) 介绍:其函数在Oracle 10g推出,在10g版本中,返回字符串类型,在11g版本中返回clob类型.括号里面的参数是列,而且可以是多个列的集 ...