LoppA's blog

By LoppA, history, 7 years ago, In English

Hi, in this problem from HackerRank we abstract the problem to a directed graph with capacity and cost on the edges, source and sink and we need to get the minimum cost despite the flow (as long as we don't exceed the capacities).

To achieve that, we just need to change the Max Flow Min Cost algorithm breaking it when the best augmenting path we found has total cost greater than zero. Link to editorial: here (he changed the problem to Maximum Cost so he breaks it when the best augmenting path has cost smaller than zero).

My doubt is: Does this algorithm to get Minimum Cost with "Any Flow" work specifically for this problem's class of graph or does it work for every other cost-flow graph?

  • Vote: I like it
  • +6
  • Vote: I do not like it

»
7 years ago, # |
  Vote: I like it +10 Vote: I do not like it

I belive you can do it anywhere since there is a way to keep the same old algorithm and modify the flow network in such a way that we get the minimum cost despite the flow.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    What do you mean by modifying the flow network?

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      Here is an idea, if you have edges linked to the super-source of type (capacityi, costi), create a new node which will be the actual source and link it with the former source with an edge of type and also link this former source with the sink with the same type of edge. This way, a flow unity will never travel on a path of negative cost because it can take the source -> former source -> sink path of cost 0.

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Yes, now its clear for me that it works. I changed my code too and passed with this idea, its easier too see the correctness of the approach thinking this way.

        Thanks bciobanu!