Graph Theory37 sections · 1633 units
Open in Course

CSES 1673 High Score - Negating Weights

(Maximum becomes minimum)

To find the maximum score path, negate all edge weights and find the minimum path. If the original edge has weight ww, use weight w-w in Bellman-Ford. The shortest path in the converted graph is the longest path in the original. This works because minimizing w1w2w3-w_1 - w_2 - w_3 is the same as maximizing w1+w2+w3w_1 + w_2 + w_3.

After finding the shortest path with negated weights, negate the result to get the maximum score. If dist[n] = -10 in the converted graph, the maximum score in the original is 1010.