The longest path of a directed acyclic graph [Java implementation]

Question description

There is a directed acyclic graph with a total of n vertices and m edges (assuming that the vertices are numbered from 0 ton-1), find the maximum value of the sum of edge weights in all paths of the graph (the starting point and end point are not fixed)

Enter description

Two integers in the first linen、m(1≤n≤100,0≤m≤n(n−1)) represent the number of vertices and edges respectively;

The next linesm have three integers in each lineu、v、w(0≤u≤n−1,0≤v≤n−1,u≠v,1≤w≤100), indicating the number of the starting point and end point of an edge and the edge weight. Data guarantees that there will be no duplicate edges

Output description

Output an integer representing the maximum value of the sum of edge weights on the path

Sample

enter

6 7
0 1 1
0 2 2
1 3 3
1 4 4
2 4 2
3 5 2
4 5 3

output

8
image-20230312104401240

Idea analysis

  • The question does not limit the starting point and end point, so according to the principle ofgreedy, if there is a path, you should try to walk it, and at the same time, you should put each Use the vertex as the starting point to try again
  • If we can know the maximum sum of weights starting from the current vertexi, then select the maximum value by comparing the sum of each weightmaxVal , can be used as our answer. So the key to the problem is: how to record the maximum sum of weights starting from the current vertexi? From this we can consider usingdynamic programming
  • settingg[n][n] is used to store the relationship between vertices and edges, settingdp array, where dp[i] represents the maximum sum of weights that can be obtained starting from the current vertex i
  • Assuming that there is a path between the current vertexi and the vertexj, then there are only two values ​​​​from dp[i] kind:
    1. dp[i] itself is the sum of the currently known weights that can be obtained starting from i. However, since our processing of dp is not complete yet, this The sum of weights is not necessarily the largest
    2. dp[j] + g[i][j], that is, the sum of the weights from vertex i to vertex j plus the weights that can be obtained starting from j
    3. Select the largest one as the true value ofdp[i] [At this time, i is fixed, and j is changing 】
  • An optimization can be done here, that is, when dp[i]>0, it returns directly to dp[i], because at this time it means that dp[i] has been After being processed, the internal storage is the maximum value starting from the i point, and there is no need to repeat the processing
  • Finally, you can get the overall processed arraydp, and the maximum value is our answer

Code

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

public class Main {
    
    

	public static void main(String[] args) {
    
    
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int m = scanner.nextInt();
		// 存放地图
		int g[][] = new int[n][n];
		// dp[i]代表从 i 出发能够得到的最大权重之和
		int dp[] = new int[n];
		// 对图进行初始化
		for (int i = 0; i < m; i++) {
    
    
			int u = scanner.nextInt();
			int v = scanner.nextInt();
			int w = scanner.nextInt();
			g[u][v] = w;
		}
		int maxVal = 0;
		for (int i = 0; i < n; i++) {
    
    
			maxVal = Math.max(maxVal, getDAGMaxLength(g, dp, i, n));
		}
		System.out.println(maxVal);

	}
	
	// 返回从 i 出发的有向无环图最大权值之和路径
	public static int getDAGMaxLength(int g[][], int dp[], int i, int n) {
    
    
		// 说明 i 点已经处理过,不需要重复计算
		if (dp[i] > 0) {
    
    
			return dp[i];
		}
		for (int j = 0; j < n; j++) {
    
    
			// i 到 j 有路可走
			if (g[i][j] > 0) {
    
    
				// 看看从 j 出发的最大权值之和 + 当前顶点 i 到 j 的权重是否大于直接从 i 出发的最大权值之和
				dp[i] = Math.max(dp[i], getDAGMaxLength(g, dp, j, n) + g[i][j]);
			}
		}
		return dp[i];
	}

}

Guess you like

Origin blog.csdn.net/weixin_45488428/article/details/129481986