To solve a min cost problem, I am using SPFA algorithm in the link below. https://konaeakira.github.io/posts/using-the-shortest-path-faster-algorithm-to-find-negative-cycles