Question description
There is a directed acyclic graph with a total of
n
vertices andm
edges (assuming that the vertices are numbered from0
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 line
n、m(1≤n≤100,0≤m≤n(n−1))
represent the number of vertices and edges respectively;The next lines
m
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
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 vertex
i
, 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 - setting
g[n][n]
is used to store the relationship between vertices and edges, settingdp
array, wheredp[i]
represents the maximum sum of weights that can be obtained starting from the current vertexi
- Assuming that there is a path between the current vertex
i
and the vertexj
, then there are only two values fromdp[i]
kind:dp[i]
itself is the sum of the currently known weights that can be obtained starting fromi
. However, since our processing ofdp
is not complete yet, this The sum of weights is not necessarily the largestdp[j] + g[i][j]
, that is, the sum of the weights from vertexi
to vertexj
plus the weights that can be obtained starting fromj
- Select the largest one as the true value of
dp[i]
[At this time,i
is fixed, andj
is changing 】
- An optimization can be done here, that is, when
dp[i]>0
, it returns directly todp[i]
, because at this time it means thatdp[i]
has been After being processed, the internal storage is the maximum value starting from thei
point, and there is no need to repeat the processing - Finally, you can get the overall processed array
dp
, 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];
}
}