Shortest Path Constrained

Правка en1, от AakashHanda, 2019-02-04 13:31:58

I decided to write this blog, as a lot of people were requesting for the editorial of Shortest Path Constrained problem from CodeNation Hiring Test, held on 26th January, 2019

Problem

Statement

There is a weighted undirected graph with N nodes and M edges. You are also given X different sets of nodes — S1 to Sx. You have to give the shortest possible distance between a start node — a and a destination node — b, along a path following the following rule: For all i < j, before visiting a node in Sj, you must visit all the nodes in Si.

The distance is defined as sum of weights of edges along the path traversed.

Note :-

  1. The path must contain all the nodes in union of Si for all 1 ≤ i ≤ X
  2. for all 1 ≤ i, j ≤ X and i ≠ j.
  3. The path may go through any node, including a and b, more than once. The constraint is that it must end at b

Constraints

  • 2 ≤ N ≤ 100
  • 0 ≤ w ≤ 100000
  • 0 ≤ X ≤ 10
  • 1 ≤ |Si| ≤ 10 for all i ≤ X
  • Nodes are numbered from 0 to N - 1
  • The graph does not contains multiple edges or self edges, and is connected

Solution

Let us first solve the problem of how to visit all the nodes in a set Si, in shortest possible distance, such that we end up at node x. This problem can be solved using dp + bitmask. Suppose dp[bitmask][x] stores the minimum distance, for this particular set, such that the nodes with bits set to 1 in bitmask are visited, and we end up at node x. Now for all k such that kth bit is 0 in bitmask, we can update dp[bitmask|k][k] = min(dp[bitmask][x] + dist(x, k), dp[bitmask|k][k])

Теги dp bitmask, #graph theory, floyd-warshall, #editorial

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en9 Английский AakashHanda 2019-02-04 14:01:08 4
en8 Английский AakashHanda 2019-02-04 13:59:49 0 (published)
en7 Английский AakashHanda 2019-02-04 13:59:30 1
en6 Английский AakashHanda 2019-02-04 13:58:19 1 Tiny change: '========\n\n<spoiler' -> '========\n<spoiler'
en5 Английский AakashHanda 2019-02-04 13:56:50 29 Tiny change: ' for $S_i$\n\n\nCode' -> ' for $S_i$, as shown in the code below.\n\n\nCode'
en4 Английский AakashHanda 2019-02-04 13:55:58 22
en3 Английский AakashHanda 2019-02-04 13:54:30 48
en2 Английский AakashHanda 2019-02-04 13:52:51 4311 Tiny change: ' summary="Problem">\n### St' -> ' summary="Click to Expand">\n### St'
en1 Английский AakashHanda 2019-02-04 13:31:58 1795 Initial revision (saved to drafts)