Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a weighted graph and natural number k how to find the cheapest path from node s to t that can be divided by k?

Given a weighted graph G=(V,E) which doesnt include negative cycles, a natural number k, and two verticles: s,t.

How can I find the cheapest route from s to t which its length can be divied by k?

like image 948
cstudent1212 Avatar asked Nov 16 '25 10:11

cstudent1212


1 Answers

Prepare a new graph G' with vertices V × {0, 1, …, n−1} and for each arc v → w of length ℓ in G, arcs (v, x) → (w, (x + ℓ) mod k). Then use Dijkstra's algorithm to find a shortest path from (s, 0) to (t, 0).

like image 151
David Eisenstat Avatar answered Nov 19 '25 09:11

David Eisenstat