We have a graph and we want to minimize B × dist(1,m) + E × dist(2,m) + P × dist(m,N) By running three Dijktra we can precompute the values dist(1,m), dist(2,m) and dist(N,m) for all possible m. Once that is done we just need to consider each possible m.