Bellman_ford POJ 3259 Wormholes
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 41728 | Accepted: 15325 |
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.
就是当你过去的时候时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。
简化下,就是看图中有没有负权环。
#include <cstdio>
#define maxn 502
#define INF 0x3fffffff ///边
typedef struct Edge{
int u, v; ///起点, 终点
int weight; ///权值
}Edge; Edge edge[]; ///双向边,保存边的权值
int dist[maxn]; ///节点到原点的最小距离
int edgenum; ///边数
///插入边
void insert(int u, int v, int w)
{
edge[edgenum].u = u;
edge[edgenum].v = v;
edge[edgenum++].weight = w;
}
bool Bellman_Ford(int source, int nodenum) ///原点和结点个数
{
for(int i=; i < nodenum; ++i)
dist[i] = INF;
dist[source] = ;
for(int i=; i < nodenum; ++i){
for(int j=; j < edgenum; ++j)
{
if(dist[edge[j].v] > dist[edge[j].u] + edge[j].weight)///松弛计算
dist[edge[j].v] = dist[edge[j].u] + edge[j].weight;
}
}
bool flag = false;
/// 判断是否有负权环
for(int i=; i < edgenum; ++i){
if(dist[edge[i].v] > dist[edge[i].u] + edge[i].weight)
{
flag = true; ///有负权环
break;
}
}
return flag;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, m, c;
scanf("%d%d%d", &n, &m, &c);
edgenum = ;
for(int i=; i<m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
insert(u, v, w);
insert(v, u, w);
}
for(int i=; i<c; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
insert(u, v, -w);
}
if(Bellman_Ford(, n))
printf("YES\n");
else
printf("NO\n");
}
return ;
}
Bellman_ford POJ 3259 Wormholes的更多相关文章
- 最短路(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 (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- poj 3259 Wormholes
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- ajax验证用户名和密码
var user = form.name.value; var password = form.password.value; var url = "chkname.php?user=&qu ...
- 原生Js获取某个节点后面的第一个标签
nextSlbling属性 获取某个节点后面的第一个节点(可能是标签 文本) 判断获取的节点是否为标签节点还是文本节点 window.onload=function(){ var pagecount= ...
- delphi定义自己的消息
定义一个消息需要两个步骤: 1.声明一个消息标识符 2.声明一个消息记录类型 一个消息标识符是一个整数大小的常数.Windows自用低于1024的消息,所以当你声明你自己的消息,你应该开始高于这一数字 ...
- python变量不能以数字打头
在编写python函数时,无意中发现一个问题:python中的变量不能以数字打头,以下函数中定义了一个变量3_num_varchar,执行时报错. 函数如下: def database_feild_v ...
- C++实现红黑树,仿STL封装
//RB_Tree.hpp //The code of red black trees //2011/12/31 by Adoo // The foundation :http://www.roadi ...
- Unity3d 项目管理
Unity3d 项目管理 1.项目管理采用TortoiseSVN方式进行管理,但是也要结合人员管理的方式,尽量在U3D中多采用Scene(关卡的方式),以一下目录的方式进行管理! 以名字的方式进行合 ...
- PCB板可靠性测试方法择要
在电子设备中PCB板是所有电子设备的核心,其的可靠性程度会直接影响了产品的耐用性和寿命.因此在我们实验室(上海摩尔实验室)的实际工作中遇到了越来越多的针对PCB板的可靠性的测试要求,现根据一些企业的内 ...
- struts2令牌,防止重复提交
struts2的令牌,可以用来防止重复提交,其原理是在提交jsp页面中,写入一个隐藏域name="token",然后在action中定义一个变量token并get.set.在服务器 ...
- 杂题 UVAoj 10000 Longest Paths
Longest Paths It is a well known fact that some people do not have their social abilities complet ...
- Delphi调用webservice总结
Delphi调用webservice总结 Delphi调用C#写的webservice 用delphi的THTTPRIO控件调用了c#写的webservice. 下面是我调试时遇到的一些问题: ...