Bellman Ford algorithm and example java code template

Keywords: Algorithm Dynamic Programming

matters needing attention:

If there is a negative weight loop in the diagram, there is not necessarily the shortest circuit. Because if the infinite loop in the negative weight loop finally comes out, the shortest path is negative infinity.

For example, the following figure:

But what we said above is that if there is a negative weight loop, there may not be the shortest circuit. Why? Because as long as the negative weight loop is not on the path from the initial point to the end point, the negative weight loop has no effect on the shortest circuit.

Thought:

① The first layer circulates n times, and N represents the number of nodes.

② The second layer circulates m times, and M represents the number of edges. Each inner loop compares the relationship between the distance stored at the end of the current edge and the distance stored at the start of the current edge + the length of this edge. If the distance stored at the end is greater than the distance stored at the start + the length of this edge, the distance stored at the end will be updated.

As shown in the following figure, the update will occur.  

code:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int[][] way = new int[10010][3];// Storage edge
	static int[] dist = new int[510];// Distance to a point
	static int[] temp = new int[3];// Store single edge
	static int n, m, k;

	public static void Bellman_Ford() {
		Arrays.fill(dist, Integer.MAX_VALUE / 10);// initialization
		dist[1] = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				temp = way[j];//Get edge
				dist[temp[1]] = Math.min(dist[temp[1]], dist[temp[0]] + temp[2]);//to update
			}
		}
	}
	
	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		n = input.nextInt();
		m = input.nextInt();
		k = input.nextInt();
		for (int i = 0; i < m; i++) {
			way[i][0] = input.nextInt();
			way[i][1] = input.nextInt();
			way[i][2] = input.nextInt();
		}
		Bellman_Ford();
		if (dist[n] > Integer.MAX_VALUE / 20)
			System.out.println("There is no shortest path");
		else
			System.out.println(dist[n]);
	}
}

Example:

Given a   n   Points   m   There may be multiple edges and self rings in a directed graph with one edge,   The edge weight may be negative.

Please find out from   one   No. 1 point to   n   Maximum number of points passed   k   The shortest distance of an edge, if it cannot be   one   Go to the station on the th   n   Point number, output   impossible.

Note: there may be   There is a negative weight loop  .

Input format

The first line contains three integers   n,m,k.

next   m   Rows, each containing three integers   x. Y, Z, indicates that there is a slave point   x   To point   y   The directed edge of is   z.

Output format

Outputs an integer representing the   one   No. 1 point to   n   Maximum number of points passed   k   The shortest distance of an edge.

If there is no path that meets the condition, output   impossible.

Data range

1≤n,k≤500,
1≤m≤10000,
The absolute value of any side length shall not exceed   10000.

Input example:

3 3 1
1 2 1
2 3 1
1 3 3

Output example:

3

Problem solving Code:

import java.util.Arrays;
import java.util.Scanner;


public class Main {
	static int[][] way = new int[10010][3];// Storage edge
	static int[] dist = new int[510];// Distance to a point
	static int[] last = new int[510];// ***Store the distance from each point of the last cycle***
	static int[] temp = new int[3];// Store single edge
	static int n, m, k;

	public static void Bellman_Ford() {
		Arrays.fill(dist, 50000);// initialization
		dist[1] = 0;
		for (int i = 0; i < k; i++) {
			last = Arrays.copyOfRange(dist, 0, dist.length);// Copy the last result to last
			for (int j = 0; j < m; j++) {
				temp = way[j];
				dist[temp[1]] = Math.min(dist[temp[1]], last[temp[0]] + temp[2]);
			}
		}
	}

	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		n = input.nextInt();
		m = input.nextInt();
		k = input.nextInt();
		for (int i = 0; i < m; i++) {
			way[i][0] = input.nextInt();
			way[i][1] = input.nextInt();
			way[i][2] = input.nextInt();
		}
		Bellman_Ford();
		if (dist[n] > 25000)// There is a negative weight edge, which cannot be directly equal to
			System.out.println("impossible");
		else
			System.out.println(dist[n]);
	}
}

Posted by sqishy on Fri, 19 Nov 2021 02:39:43 -0800