POJ-3259(最短路+Bellman-Ford算法判负圈)
Wormholes
POJ-3259
- 这题是最短路问题中判断是否存在负圈的模板题。
- 判断负圈的一个关键就是理解:如果在图中不存在从s可达的负圈,最短路径不会经过一个顶点两次。while循环最多执行v-1次。
- 这题还需要注意的是输入的w条边的权值是负值,因为这是虫洞边。
package POJ;
import java.util.*;
public class POJ_3259 {
static int f,n,m,w;
public int from,to;
public double rate,commisions;
static int edges;//边数
static class edge{
public int from,to,se;
edge(){}
edge(int from,int to,int se){
this.from=from;this.to=to;this.se=se;
}
};
static edge []es;
static int []d;//distance数组
static boolean BellmanFord() {
Arrays.fill(d, 0);
for(int i=0;i<n;i++) {//下标从1开始
for(int j=0;j<edges;j++) {
edge e=es[j];
if(d[e.to]>d[e.from]+e.se) {
d[e.to]=d[e.from]+e.se;
if(i==n-1)
return true;
}
}
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
f=cin.nextInt();
while(f!=0) {
n=cin.nextInt();
m=cin.nextInt();
w=cin.nextInt();
es=new edge[2*m+w];
d=new int[n+1];
int k=0;
int from,to,se;
for(int j=0;j<m;j++) {
from=cin.nextInt();
to=cin.nextInt();
se=cin.nextInt();
es[k++]=new edge(from,to,se);
es[k++]=new edge(to,from,se);
}
for(int j=0;j<w;j++) {
from=cin.nextInt();
to=cin.nextInt();
se=cin.nextInt();
es[k++]=new edge(from,to,-se);
}
edges=k;
if(BellmanFord())
System.out.println("YES");
else System.out.println("NO");
f--;
}
}
}
POJ-3259(最短路+Bellman-Ford算法判负圈)的更多相关文章
- 【原创】POJ 3259 Wormholes(Bellman-Ford) && 简介Bellman-Ford算法
[原创] 题目大意 John有N个农场,一共有M条边,在农场上出现了W个虫洞(W是一条边),其中M是双向普通边,W是单向虫洞边.John穿行于农场之间每经过一条边(S到E)的时间为+T,每经过虫洞会时 ...
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- LightOJ-1074(SPFA判负圈+Bellman-Ford算法)
Extended Traffic LightOJ-1074 这题因为涉及到减法和三次方,所以可能会出现负圈. 这里使用的算法叫做SPFA算法,这个可以用来判负圈和求解最短路.Bellman-Ford算 ...
- AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)
题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...
- UVA 11090 判负圈问题
题目链接http://vjudge.net/problem/viewProblem.action?id=34650 题目大意: 给定n个点m条边的加权有向图,求平均权值最小的回路.平均权值=路径权值之 ...
- AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)
题目链接: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B Single Source Shortest Path ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 2240 Arbitrage (Bellman Ford判正环)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions:27167 Accepted: 11440 Descri ...
随机推荐
- zoj3299 Fall the Brick
Time Limit: 3 Seconds Memory Limit: 32768 KB Now the God is very angry, so he wants to punish t ...
- markdown 公式编写及不同平台公式转换
1.markdown 用法及公式编写,这块就不再重复,已有很多官方平台的文档说明很完善 有道云markdown写作文档 在博客园中插入公式 markdown公式输入(特殊符号) markdown 特殊 ...
- Leetcode(14)-最长公共前缀
编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...
- SpringBoot进阶教程(七十)SkyWalking
流行的APM(Application Performance Management工具有很多,比如Cat.Zipkin.Pinpoint.SkyWalking.优秀的监控工具还有很多,其它比如还有za ...
- 微服务架构Day05-SpringBoot之Servlet
旧版 配置嵌入式Servlet容器 SpringBoot默认使用Tomcat作为嵌入式Servlet容器 如何定制和修改Servlet容器相关配置 1.在配置文件中定制和修改Servlet容器有关的配 ...
- React Hooks: useImperativeHandle All In One
React Hooks: useImperativeHandle All In One useImperativeHandle https://reactjs.org/docs/hooks-refer ...
- Learning Web Performance with MDN
Learning Web Performance with MDN Web 性能是客观的衡量标准,是加载时间和运行时的感知用户体验. https://developer.mozilla.org/en- ...
- 联合登录 & OAuth 2.0 & OpenID
联合登录 & OAuth 2.0 & OpenID 第三方联合登录一般可以降低网站的获客成本,所以一般的网站都会做一些联合登录,常用的就是QQ.微信.微博; https://www.z ...
- Array.fill & String.padStart & String.padEnd
Array.fill & String.padStart & String.padEnd Array.fill arr.fill(value[, start[, end]]) http ...
- Flutter CodePen challenges
Flutter CodePen challenges 挑战赛 https://mp.weixin.qq.com/s/qIYokWN9SVgr-F7YxbJuOQ CodePen Flutter 编辑器 ...