应用
单源最短路
力扣743求某点到所有点的最短路的最大值。
public class BellmanFord743 {
class Edge {
int from;
int to;
int cost;
Edge(int from, int to, int cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
}
int[] dis;
Edge[] edges;
private void init(int[][] times, int n, int k){
dis = new int[n+1];
Arrays.fill(dis, -1);
edges = new Edge[times.length];
for (int i = 0; i < times.length; i++) {
edges[i] = new Edge(times[i][0], times[i][1], times[i][2]);
}
dis[k] = 0;
}
private void bellmanFord(int n) {
for (int i = 1; i < n; i++){
for (Edge edge : edges) {
if (dis[edge.to] != -1 && dis[edge.from] != -1 && dis[edge.from] + edge.cost < dis[edge.to]) {
dis[edge.to] = dis[edge.from] + edge.cost;
} else if (dis[edge.to] == -1 && dis[edge.from] != -1) {
dis[edge.to] = dis[edge.from] + edge.cost;
}
}
}
}
private int calculate(){
int res = -1;
for (int i = 1;i<dis.length;i++) {
int di = dis[i];
if (di == -1){
return -1;
} else {
res = Math.max(res,di);
}
}
return res;
}
public int networkDelayTime(int[][] times, int n, int k) {
init(times,n,k);
bellmanFord(n);
return calculate();
}
public static void main(String[] args) {
BellmanFord743 bellmanFord743 = new BellmanFord743();
System.out.println(bellmanFord743.networkDelayTime(new int[][]{{2, 1, 1}, {2, 3, 1}, {3, 4, 1}}, 4, 2));
}
}
判断负权环
最多经过k条边的最短路
力扣787 求经过有限点的最短路
public class BellmanFord787 {
private final static int UN_REACH = -1;
class Edge {
int from;
int to;
int cost;
Edge(int from, int to, int cost) {
this.from = from;
this.to = to;
this.cost = cost;
}
}
class Node {
int dis;
int history;
Node() {
this.dis = UN_REACH;
this.history = UN_REACH;
}
}
// 图中的边信息
Edge[] edges;
// 源点src到下标index的最短距离
Node[] nodes;
/**
* 初始化
*/
private void init(int n, int[][] flights, int src) {
edges = new Edge[flights.length];
nodes = new Node[n];
for (int i = 0; i < flights.length; i++) {
edges[i] = new Edge(flights[i][0], flights[i][1], flights[i][2]);
}
for (int i = 0; i < n; i++) {
nodes[i] = new Node();
}
// src到src的最短距离为0
nodes[src].dis = 0;
nodes[src].history = 0;
}
/**
* 从src到dst最多经过k个节点的最短距离。不存在则返回-1
* @param n 节点数
* @param flights 两点数据 [from,to,cost]
* @param src 起点
* @param dst 终点
* @param k 经过的节点数
* @return 最短距离
*/
public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
init(n, flights, src);
// 经过k个节点,即经过k条边。如果松弛k+1条边都没有从src到dst的最短距离,则不可达
// 更新i轮会让src距离i条边以内的节点具有最短路径。
for (int i = 1; i <= k+1; i++) {
// 遍历每条边
for (Edge edge : edges) {
int from = edge.from;
int to = edge.to;
int cost = edge.cost;
// 使用history的目的是防止更新src出发i+1条边之后的节点。
if (nodes[to].dis == UN_REACH) {
if (nodes[from].history != UN_REACH) {
// 如果这条边的目标点未得到距离,并且发起点有距离,则直接极端目标点的距离
nodes[to].dis = nodes[from].history + cost;
}
} else if (nodes[from].history != UN_REACH && nodes[from].history + cost < nodes[to].dis) {
// 目标点、发起点都有距离,则取较小值
nodes[to].dis = nodes[from].history + cost;
}
}
// 本轮松弛结束后,更新同步历史值为最新值
for (int j = 0; j < n; j++) {
nodes[j].history = nodes[j].dis;
}
}
return nodes[dst].dis;
}
}