POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接:
|
Wormholes
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 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 Input
Line 1: A single integer, F. F farm descriptions follow.
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 Ethat 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 Ethat 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 Sample Output NO 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. Source |
题意:
田地间有 M 条路径 【双向】(1<= M <= 2500)
同时有 W 个孔洞,可以回到以前的一个时间点【单向】(1<= W <=200)
问:FJ 是否能在田地中遇到以前的自己
算法:bellman_ford 判断是否有负环
思路:
孔洞间的单向路径加边,权值为负【可以回到以前】
判断有向图是否存在负环
因为如果存在了负数环,时间就会不停的减少,
那么 FJ 就可以回到以前更远的地方,肯定能遇到以前的自己的
code:
/********************************************************************
Accepted 180 KB 47 ms C++ 2509 B
题意:农夫 FJ 有 N 块田地【编号 1...n】 (1<=N<=500)
田地间有 M 条路径 【双向】(1<= M <= 2500)
同时有 W 个孔洞,可以回到以前的一个时间点【单向】(1<= W <=200)
问:FJ 是否能在田地中遇到以前的自己
算法:bellman_ford 判断是否有负环
思路:田地间的双向路径加边,权值为正
孔洞间的单向路径加边,权值为负【可以回到以前】
判断有向图是否存在负环
因为如果存在了负数环,时间就会不停的减少,
那么 FJ 就可以回到以前更远的地方,肯定能遇到以前的自己的
*******************************************************************/
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 510;
const int maxw = 2500*2+200+10;
const int INF = 10000;
int d[maxn];
int n,m; struct Edge{
int u,v;
int t;
}edge[maxw]; bool bellman_ford()
{
for(int i = 1; i <= n; i++) d[i] = INF; //初始化从起点到 i 时间为最值
d[1] = 0; //起点为 0 for(int i = 1; i < n; i++)
{
bool flag = true; //判断这轮是否能够松弛
for(int j = 0; j < m; j++)
{
int u = edge[j].u;
int v = edge[j].v;
int t = edge[j].t; if(d[v] > d[u]+t) //松弛操作
{
d[v] = d[u]+t;
flag = false;
}
}
if(flag) return false; //如果当前轮不能松弛,直接判断没有负数环
} for(int i = 0; i < m; i++)
{
if(d[edge[i].v] > d[edge[i].u]+edge[i].t)
return true;//如果仍然能够松弛则存在负环
}
return false;
} int main()
{
int T;
int M,W;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &n,&M,&W);
m = 0; int u,v,t;
for(int i = 1; i <= M; i++) //田地间的大路,加双边
{
scanf("%d%d%d", &u,&v,&t);
edge[m].u = u;
edge[m].v = v;
edge[m++].t = t; edge[m].u = v;
edge[m].v = u;
edge[m++].t = t;
} for(int i = 1; i <= W; i++) //孔洞,加单边
{
scanf("%d%d%d", &u,&v,&t);
edge[m].u = u;
edge[m].v = v;
edge[m++].t = -t;
} if(bellman_ford()) printf("YES\n"); //存在负数环
else printf("NO\n");
}
}
POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】的更多相关文章
- POJ 3259 Wormholes ( SPFA判断负环 && 思维 )
题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...
- POJ 3259 Wormholes(SPFA判负环)
题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...
- POJ 3259 Wormholes 最短路+负环
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...
- POJ 3259 Wormholes( bellmanFord判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36425 Accepted: 13320 Descr ...
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
- POJ 3259 Wormholes【Bellman_ford判断负环】
题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环 因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说 ...
- POJ 3259:Wormholes bellman_ford判定负环
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37906 Accepted: 13954 Descr ...
- poj 3259 (Bellman_Ford判断负环)
题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
随机推荐
- attempt to dereference a generic a pointer(转)
在Linux下的GDB环境中,用p 命令查看一个void *型的变量的时候,提示为: "attempt to dereference a generic a ...
- 【Python3 爬虫】09_正则表达式(re.math()、re.search()、re.sub()、全局匹配函数)
re.math()函数 从源字符串的起始位置匹配一个模式 语法:re.match(pattern, string, flag) 第一个参数代表对应的正则表达式,第二个参数代表对应的源字符,第三个参数是 ...
- 【设计模式 7】从公司的目前框架和API Gateway,谈谈对外观模式的理解
我,第一次用到外观模式,应该是3年多以前.那时候是做一个收费系统,在当时的U层和B层之间,加了一层Facade.当时,在一些复杂的业务逻辑处理时,感受到了加入外观层的好处,但对于一些简单的(我指的是, ...
- Autofc与Mvc,WebForm,Weiapi,Owin整合源码分析
主要分析一下的几个项目: Autofac.Integration.Mvc Autofac.Integration.WebApi Autofac.Integration.Owin Autofac.Int ...
- Javascript 与 SPA单页Web富应用
书单推荐 # <单页Web应用:JavaScript从前端到后端> http://download.csdn.net/detail/epubitbook/8720475 # <MVC ...
- SVN Client API的.net 接口 SharpSvn介紹 Checkout操作实例
Subversion是一個文件版本管理工具, 廣泛的被大家採用來作為源代碼版本管理. 已有的工具不管是其自帶的命令行工具還是Windows UI的tortoiseSVN等還是很方便實用的, 但是如果想 ...
- 使用CSS3实现响应式标题全屏居中和站点前端性能
要实现标题全屏居中(同一时候在垂直和水平方向居中).有若干种方法,包含使用弹性布局.表格单元.绝对定位和自己主动外边距等. 全屏居中 当中眼下比較流行也比較easy理解的方法是使用绝对定位+偏移实现. ...
- Atitit.常用语言的常用内部api 以及API兼容性对源码级别可移植的重要性 总结
Atitit.常用语言的常用内部api 以及API兼容性对源码级别可移植的重要性 总结 1.1. 要兼容的重要语言api1 1.2. 常用基础api分类 core api1 1.3. 比较常用的扩展库 ...
- Django学习之raw()方法查询数据
我们经常有这种需求: 用sql来查询以及写入数据到数据库,Django当然也提供了这种方式,那就是通过raw方法: sql = "select * from blog_blog where ...
- 关于public class
初学问题:“The public type movietestdive must be defined in its own file” 对于一个class里,只能出现一个public class(公 ...