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 ...
随机推荐
- 如何在滚动报表时保持标题可见 (Reporting Services)
From: https://msdn.microsoft.com/zh-cn/library/bb934257.aspx 对于跨多页的表或矩阵数据区域,可以控制滚动报表时是否始终显示包含列标题的初始行 ...
- 戴文的Linux内核专题:04安全
转自Linux中国 Linux内核是所有Linux系统的核心.如果有任何恶意代码控制或破害了内核的任何一部分,那么系统会严重受损,文件可能被删除或损坏,私人信息可能被盗等等.很明显,保持内核安全涉及到 ...
- MongoDB的C#驱动程序教程(译) 转
1.概述 本教程是10gen支持C#驱动程序MongoDB的介绍.假定您熟悉使用MongoDB,因此主要集中在如何使用C#访问MongoDB的. 它分为两个部分:C# 驱动程序 ,BSON图书馆.C# ...
- the way of reading English books
除了datesheet ,我们经常遇到英语文档和资料.找到正确的英语文档的打开方式变的非常必要. 计算机类的书大致归为三大类: (1)语言方面的书.比如C.Java.Python等等 (2)算法书籍. ...
- openstack中运行定时任务的两种方法及源代码分析
启动一个进程,如要想要这个进程的某个方法定时得进行执行的话,在openstack有两种方式: 一种是通过继承 periodic_task.PeriodicTasks,另一种是使用loopingcall ...
- HTML的窗口分帧
下面通过一个后台管理的部分设计来说明窗口分帧 frameset.html代码 <!-- <frameset>标签(常用来做后台管理界面) 属性:rows(行).cols(列).可以使 ...
- 15、C#基础整理(递归)
带输出参数的函数 输入参数相当于函数而言,相当于已经赋值了的变量,直接可用输出参数相当于定义一个没有值的变量,在函数中进行赋值,然后调用函数的时候将赋值带出函数 例: public void shuc ...
- POJ3974 (manacher)
var s,t:ansistring; n,op:longint; p:..] of longint; procedure pre; var i:longint; begin s:='$*'; to ...
- Bat脚本处理ftp超强案例解说
Bat脚本处理ftp超强案例解说 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://369369.blog.51cto.com/31 ...
- Selenium - 实现网页元素拖拽
Drag and Drop, 使用鼠标实现元素拖拽的操作貌似很复杂, 在Selenium中, 借助OpenQA.Selenium.Interactions.Actions类库中提供的方法, 实现起来还 ...