Skip to content

Commit

Permalink
Merge pull request #65 from qryxip/fix-mincostflow
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip authored Oct 2, 2020
2 parents ad148b0 + 861f2f7 commit 3056e2a
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/mincostflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ where
let mut prev_e = vec![0; n];
let mut flow = T::zero();
let mut cost = T::zero();
let mut prev_cost: Option<T> = None;
let mut prev_cost_per_flow: Option<T> = None;
let mut result = vec![(flow, cost)];
while flow < flow_limit {
if !self.refine_dual(source, sink, &mut dual, &mut prev_v, &mut prev_e) {
Expand All @@ -113,11 +113,11 @@ where
let d = -dual[source];
flow += c;
cost += d * c;
if prev_cost == Some(d) {
if prev_cost_per_flow == Some(d) {
assert!(result.pop().is_some());
}
result.push((flow, cost));
prev_cost = Some(cost);
prev_cost_per_flow = Some(d);
}
result
}
Expand Down Expand Up @@ -198,4 +198,15 @@ mod tests {
assert_eq!(flow, 2);
assert_eq!(cost, 6);
}

#[test]
fn same_cost_paths() {
// https://github.com/atcoder/ac-library/blob/300e66a7d73efe27d02f38133239711148092030/test/unittest/mincostflow_test.cpp#L83-L90
let mut graph = MinCostFlowGraph::new(3);
assert_eq!(0, graph.add_edge(0, 1, 1, 1));
assert_eq!(1, graph.add_edge(1, 2, 1, 0));
assert_eq!(2, graph.add_edge(0, 2, 2, 1));
let expected = [(0, 0), (3, 3)];
assert_eq!(expected[..], *graph.slope(0, 2, i32::max_value()));
}
}

0 comments on commit 3056e2a

Please sign in to comment.