Set the default amount
amt = INF NEW_LINE x = sink NEW_LINE while x != src : NEW_LINE INDENT amt = min ( amt , flow [ x ] [ dad [ x ] ] if ( flow [ x ] [ dad [ x ] ] != 0 ) else cap [ dad [ x ] ] [ x ] - flow [ dad [ x ] ] [ x ] ) NEW_LINE x = dad [ x ] NEW_LINE DEDENT x = sink NEW_LINE while x != src : NEW_LINE INDENT if ( flow [ x ] [ dad [ x ] ] != 0 ) : NEW_LINE INDENT flow [ x ] [ dad [ x ] ] -= amt NEW_LINE totcost -= amt * cost [ x ] [ dad [ x ] ] NEW_LINE DEDENT else : NEW_LINE INDENT flow [ dad [ x ] ] [ x ] += amt NEW_LINE totcost += amt * cost [ dad [ x ] ] [ x ] NEW_LINE DEDENT x = dad [ x ] NEW_LINE DEDENT totflow += amt NEW_LINE