There is an undirected weighted graph. And also, the specified number of possible changes in the weight of the edges of the graph is K and the cost by which the edge weight can be replaced is M.
The task is to calculate what the maximum cost of the passage from the very first vertex to all the others can be achieved with the help of a fixed number of replacements K with cost M. And find the minimum number of substitutions to achieve such a result with the numbers of all modified edges.
For example, there is a graph (it enters the program as edges, each line is a new edge. The very first one contains three numbers N - the number of vertices, K and M ):
7 1 2 1 2 3 1 3 2 1 4 4 1 5 1 6 5 15 7 5 10 For him, the answer will be:
- maximum fare from 1 to all others - 40
- Number of replacements required - 1
- edge numbers on which weights need to be replaced
Here, it is advantageous to change the weight of the edge from the 1st vertex to the 5th. The maximum cost is calculated as follows: 3 + 2 + 4 + 2 + (15 + 2) + (10 + 2) = 40
I tried to solve it this way: by traversing the graph in depth, we find the number of child vertices of each vertex, and then take from them the top with the largest number of children. But in particular cases, this approach does not work.
Prompt ways to solve the problem.
