Minimum Penalty Path Discussions | Algorithms | HackerRank
  • + 1 comment

    I am using Djikstra from the source vertex and at each step instead of adding the current distance of a node to the edge weight joining it to another I am taking bitwise OR. Then I am printing minimum distance so obtained to point B. Test cases 0-4 and 6-9 are working, rest say wrong answer. Can Someone suggest a mistake in the approach?

    • Asked to answer
      + 4 comments
      Endorsed by f2014044

      Consider this

      A ->B -> C Cost (A,B)->1 (B,C) ->2 and (A,B) ->2 (Multiple edges can exist)

      In this case your approach from A to C will give 3. But the answer is 2.

      • + 1 comment

        Thanks! I undestood the error.

        • + 1 comment

          I am using same approach, but i have answer is 2, for case A ->B -> C Cost (A,B)->1 (B,C) ->2 and (A,B) ->2, but tests cases 5,8, 10 - 13, is wrong answer. Where is error?

          • + 1 comment

            Same for me. Did you find the reason?

            • + 2 comments

              No, I can't find it.

              • + 0 comments

                fionalmatters explained why Djikstra won't work:

                https://www.hackerrank.com/challenges/beautiful-path/forum/comments/137106

      • + 0 comments

        Nice!!

      • + 1 comment

        so, there's no way to do it with djikstra?

        • + 0 comments

          I think yes

      • + 0 comments

        Thanks!!!!!!