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 ...
随机推荐
- 【技术宅11】php入门运算
//1.空bool $a=''; $b=NULL; $c=false; $d=0; $e='0'; $f=array(); $g=array(array()); $h='NULL'; var_dump ...
- 技术大牛面试 http://www.itmian4.com/forum.php?mod=viewthread&tid=3824
不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,NOI金牌,开源社区名人,三年级开始写Basic...在跪拜之余我们不禁要想,和这些牛人比,作为绝大部分技 ...
- DEDECMS栏目自定义字段添加
用到的文件: catalog_add.htm 路径:\dede\templets\ catalog_edit.htm 路径:\dede\templets\ catalog_add.php 路径 ...
- How Much Did It Rain? Winner's Interview: 1st place, Devin Anzelmo
How Much Did It Rain? Winner's Interview: 1st place, Devin Anzelmo An early insight into the importa ...
- js/bat批处理调用谷歌浏览器chrome批量打开网页测试web性能
批处理批量打开网页 其实用java就可以搞定,但是这么一个轻巧的测试,js或者bat批处理去一次性打开几百个网页测试一下页面没必要上java 两者的区别,js的话,只能打开多个浏览器实例,不方便查看效 ...
- Leetcode:Repeated DNA Sequences详细题解
题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- FFT(快速傅里叶变换):HDU 4609 3-idiots
3-idiots Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- android关于图片缩放
网上有许多关于图片缩放的demo,本人都感觉不怎么好用,最近在github看到了 一个简单的支持多指缩放图片的Android View类 gesture-imageview (地址:https://g ...
- spj题
S(SNO,SNAME,SCITY) ,P(PNO,PNAME,COLOR,WEIGHT) ,J(JNO,JNAME,JCITY),其中S表示供应商,它的各属性依次为供应商号,供应商名,供应商所在城市 ...
- Mac下Intellij IDea发布Web项目详解一
Mac下Intellij IDea发布Web项目详解一 Mac下Intellij IDea发布Java Web项目(适合第一次配置Tomcat的家伙们)详解二 Mac下Intellij IDea发布J ...