This is laborous case work:
- A < B and 0 and N-1 are joined by A -> trivial.
- B < A and 0 and N-1 are joined by B -> trivial.
- B < A and 0 and N-1 are joined by A ->
Do BFS on graph with only K edges. -
A < B and 0 and N-1 are joined by B ->
You can check in O(N) time is there is a path with length 2*A (try every vertex in middle).To check other path lengths following algorithm should do the trick:
Let X(d) be set of nodes reachable by using d shorter edges from 0. You can find X(d) using following algorithm: Take each vertex v with unknown distance and iterativelly check edges between v and vertices from X(d-1). If you found short edge, then v is in X(d) otherwise you stepped on long edge. Since there are at most K long edges you can step on them at most K times. So you should find distance of each vertex in at most O(N + K) time.