POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259
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..N, M (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 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) 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 (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
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 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.
#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算法)的更多相关文章
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- poj 3259 Wormholes spfa算法
点击打开链接 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25582 Accepted: 9186 ...
- POJ 3259 Wormholes Bellman_ford负权回路
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes SPFA算法题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
随机推荐
- javascript——拖拽(完整兼容代码)
拖拽,是JS经常会用到的效果,在网上有很多的这样那样的拖拽效果,但其中往往大多有各种各养的问题,功能不全,无法兼容,而且修改的时候 也是十分麻烦. 其实拖拽的原理很简单,无非是鼠标的三个动作的解析,以 ...
- JMeter常用函数整理
"_csvRead"函数 csvRead函数是从外部读取参数,csvRead函数可以从一个文件中读取多个参数. 下面具体讲一下如何使用csvread函数: 1.新建一个csv或者t ...
- ubuntu 14.04 安装 foxit pdf阅读器
1.官网下载 http://www.foxitsoftware.cn/downloads/ 2.安装 tar -zxvf FoxitReader1.01.0925_Server_x64_enu_Set ...
- Android VersionedGestureDetector手势事件
今天研究了一下PhotoView,发现里面的自定义的手势事件可以支持所有的SDK版本,该事件可以实现拖拽.滑动.缩放功能.下面直接上代码: public abstract class Versione ...
- Android EditText email、数字验证
在做Android注册登录模块的时候,经常需要在客户端就验证用户输入的信息的正确性,如填写邮箱需要验证是否是邮箱,填写手机.年龄等信息需要验证是否是数字.先介绍一下验证邮箱的代码: /** * met ...
- Linux下screen命令
//1.列出当前的screenscreen -ls //2.新建一个screen,直接在命令行键入screen命令 screen -S [会话名称][root@www.lnuxidc.com ~]# ...
- OpenCV之响应鼠标(三):响应鼠标信息
转自:http://blog.csdn.net/haihong84/article/details/6599838 程序代碼如下: #include <cv.h>#include < ...
- JDBC专题
JDBC ·持久化(persistence):把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应用,数据持久化以为着将内存中的数据保存到硬盘上加以“固化”,而持久化的实现过程大多 ...
- 《JAVA学习笔记(14-1---14-7)》
[14-1]面向对象-继承-概述 /* //描述学生 class Student { //属性 String name; int age; //行为 void study() { System.out ...
- Ogre骨骼动画
转自:http://blog.csdn.net/yanonsoftware/article/details/1281516 OGRE的基本动画控制是很简单的,设置一个动画的操作是这样: // Set ...