📖 Explanation
This problem describes a re-weighting of edges in a graph and asks under what conditions shortest paths are preserved. The new edge weight w′(u,v) is defined as w(u,v)+f(u)−f(v).
Consider any path from a source vertex s to a destination vertex t: s=v0,v1,…,vk=t.
The original path weight is W=∑i=0k−1w(vi,vi+1).
The new path weight is W′=∑i=0k−1w′(vi,vi+1)=∑i=0k−1(w(vi,vi+1)+f(vi)−f(vi+1)).
Expanding the sum for W′, we get:
W′=∑i=0k−1w(vi,vi+1)+∑i=0k−1(f(vi)−f(vi+1))
The second summation is a telescoping sum:
∑i=0k−1(f(vi)−f(vi+1))=(f(v0)−f(v1))+(f(v1)−f(v2))+⋯+(f(vk−1)−f(vk))=f(v0)−f(vk)=f(s)−f(t).
So, W′=W+f(s)−f(t).
Since f(s)−f(t) is a constant for all paths between a given s and t, adding this constant to the path weight does not change which path is the shortest. If path A has a smaller original weight than path B (WA<WB), then WA+f(s)−f(t)<WB+f(s)−f(t), meaning path A still has a smaller new weight. This property holds true regardless of the choice of function f:V→R.