题目链接:http://poj.org/problem?id=3259

Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 45077   Accepted: 16625

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.
 
题目大意:有一个农场有n块田地,m条路连接任意两条路(无向),w个虫洞(单向)回到t秒之前,现在需要你判断她从1到n,再从n回到1是否能遇到离开前的自己
解题思路:由于题目的特殊性,只需要将穿越虫洞的时间存为负值然后用Bellman_ford算法判断是否存在负权值回路即可 如果存在说明能看到否则反之。
AC代码:
 #include <stdio.h>
#include <string.h>
#define inf 9999999
int dis[];
int p[][];
int n,m,w,ans;
struct edge
{
int x,y,z;
}g[<<];
void add(int s,int e,int t)
{
g[ans].x = s;
g[ans].y = e;
g[ans].z = t;
ans ++;
}
bool Bellman_ford()
{
int i,j;
for (i = ; i <= n; i ++)
dis[i] = inf;
dis[] = ; for (i = ; i < n; i ++)
for (j = ; j < ans; j ++)
if (dis[g[j].y] > dis[g[j].x]+g[j].z)
dis[g[j].y] = dis[g[j].x]+g[j].z; for (j = ; j < ans; j ++) //遍历所有的边
if (dis[g[j].y] > dis[g[j].x]+g[j].z)
return false;
return true;
}
int main ()
{
int s,e,t,i,j,f;
scanf("%d",&f);
while (f --)
{
ans = ;
scanf("%d%d%d",&n,&m,&w);
for (i = ; i < m; i ++)
{
scanf("%d%d%d",&s,&e,&t);
add(s,e,t);
add(e,s,t);
}
for (i = ; i < w; i ++)
{
scanf("%d%d%d",&s,&e,&t);
add(s,e,-t);
}
bool f = Bellman_ford();
if (f)
printf("NO\n");
else
printf("YES\n");
}
return ;
}

POJ 3259 Wormholes (Bellman_ford算法)的更多相关文章

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

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

  2. poj 3259 Wormholes spfa算法

    点击打开链接 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25582   Accepted: 9186 ...

  3. POJ 3259 Wormholes Bellman_ford负权回路

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  4. POJ 3259 Wormholes SPFA算法题解

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

  5. 最短路(Bellman_Ford) POJ 3259 Wormholes

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

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

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

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

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

  8. POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)

    题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...

  9. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

随机推荐

  1. POJ 3299 Humidex 难度:0

    题目链接:http://poj.org/problem?id=3299 #include <iostream> #include <iomanip> using namespa ...

  2. 12.Object-C--浅谈OC的内存管理机制

    昨天学习了OC的内存管理机制,今天想总结一下,所以接下来我要在这里bibi一下:OC的内存管理. 首先我要说的是,内存管理的作用范围. 内存管理的作用范围: 任何继承了NSObject的对象,对其他基 ...

  3. NOIP2005 篝火晚会 解题报告

    佳佳刚进高中,在军训的时候,由于佳佳吃苦耐劳,很快得到了教官的赏识,成为了“小教官”.在军训结束的那天晚上,佳佳被命令组织同学们进行篝火晚会.一共有n个同学,编号从1到n.一开始,同学们按照1,2,… ...

  4. 纯JS省市区三级联动

    代码下载

  5. [vijos P1512] SuperBrother打鼹鼠

    这周好好码树状数组和线段树!!之前没写过二维树状数组,凭借一维的思路居然写了个比较像模像样的东西出来,原来我没那么脑残.唯一要注意的就是getsum四个矩形加减的边界条件,这里看了别人标程才意识到错误 ...

  6. redis2.8--主从机同步流程

  7. JS 职责链模式

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

  8. automationOperationsWithPython

    1.psutil 系统性能信息模块,可获取系统运行的进程和系统利用率(包括CPU.内存.磁盘.网络等)信息.它主要应用于系统监控,分析和限制系统资源及进程的管理.该模块需要单独安装. 示例代码 imp ...

  9. 一个简单的Dump转文本工具—Dump2Text

    每次电脑重装都得烦心,要把庞大的IDE重新配置一次,正准备安装Visual Stdio 2010,上网找镜像的时候发现,Visual Stdio 2013推出了Community版,不仅没有lite掉 ...

  10. DB other operation

    A prepared statement is a feature used to execute the same/similar SQL statement repeatedlly with hi ...