In a multi-branch game, there are 𝑛 possible endings. You can think of it as a directed tree with 𝑛 leaf nodes, where each edge has a weight of 1. Instead of playing through the entire game to reach all possible endings, which is time-consuming, you have 𝑘 save points. What algorithm can maximize the use of these 𝑘 save points to complete all endings in the shortest possible time?
I once considered using a greedy algorithm, calculating the gain-cost ratio for each point. However, I found that the order in which the points are established affects each other. If an upstream save point is established first, the cost of establishing downstream save points decreases, but the additional gain from them also becomes smaller. This made me doubt the correctness of the greedy algorithm. And I want to know if there is a better algorithm.
Let me provide an example. Assume we have a tree structure as follows:
A————B————C
|
B1——-C1————D
|
C2————D1
If we don't use any save points, the path from A to C costs 2 steps. After reaching C, we would need to return to A. The path from A to D costs 4 steps, and the path from A to D1 costs 5 steps. Therefore, the total cost is:2(A to C)+4(A to D)+5(A to D1)=11
However, if we have a save point at B, the cost to set the save point is 1 step (since you need to move from A to B). From there, we can start again at B and visit C, D, and D1. The total cost is reduced to:1(A to B)+1(B to C)+3(B to D)+4(B to D1)=9
If we can set another save point at C1, the cost of setting the save point is 2 steps (because we don't need to go all the way from A to C1; we already have a save point at B, and the path from B to C1 is 2 steps). From there, we can start at B, go to C, then from C1 to D and D1. The total cost is reduced to:1(A to B)+2(B to C1)+1(B to C)+1(C1 to D)+2(C1 to D1)=7. And this is the minimum time .