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 ...
随机推荐
- Codeforces Round #636 (Div. 3)
比赛链接:https://codeforces.com/contest/1343 A - Candies 题意 有一数列 x + 2x + 4x + ... + 2k-1x = n,输出 k ≥ 2 ...
- Educational Codeforces Round 9 C. The Smallest String Concatenation(字符串排序)
You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them together in some or ...
- Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) C. The Delivery Dilemma (贪心,结构体排序)
题意:你要买\(n\)份午饭,你可以选择自己去买,或者叫外卖,每份午饭\(i\)自己去买需要消耗时间\(b_i\),叫外卖需要\(a_i\),外卖可以同时送,自己只能买完一份后回家再去买下一份,问最少 ...
- SQL Server 远程连接配置
打开sql server配置工具 SQL Server网络配置→SQLEXPRESS的协议→启用TCP/IP→右键属性→IP地址→IPALL端口修改为1433→重启SQL Server服务 https ...
- 统计学三大相关性系数:pearson,spearman,kendall
目录 person correlation coefficient(皮尔森相关性系数-r) spearman correlation coefficient(斯皮尔曼相关性系数-p) kendall ...
- 蓝湖 UI 设计稿上如何生成渐变色和复制渐变色
蓝湖 UI 设计稿上如何生成渐变色和复制渐变色 Sketch 生成渐变色 不要上传图片,切图 如果是切图,切图模式下就不会生成 css 代码了 复制渐变色 OK .button { width: 28 ...
- TypeScript callback Object params
TypeScript callback Object params 回调函数 对象 参数 const func = (options = {}) => { // do somthing retu ...
- CURL & Weather
CURL & Weather https://wttr.in/ $ curl wttr.in https://github.com/chubin/wttr.in refs http://www ...
- React Native & Security
React Native & Security https://reactnative.dev/docs/security React Native blogs https://reactna ...
- 微信小程序批量上传图片 All In One
微信小程序批量上传图片 All In One open-data https://developers.weixin.qq.com/miniprogram/dev/component/open-dat ...