Skip to content

Commit 6593df1

Browse files
authored
Merge pull request #89 from MiSawa/fix/mcf-initial-dist
Fix initial distance of MCF
2 parents 19509cd + 819a1fd commit 6593df1

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
Cargo.lock
3+
.idea/

src/mincostflow.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub struct MinCostFlowEdge<T> {
1111
pub struct MinCostFlowGraph<T> {
1212
pos: Vec<(usize, usize)>,
1313
g: Vec<Vec<_Edge<T>>>,
14-
cost_sum: T,
1514
}
1615

1716
impl<T> MinCostFlowGraph<T>
@@ -22,7 +21,6 @@ where
2221
Self {
2322
pos: vec![],
2423
g: (0..n).map(|_| vec![]).collect(),
25-
cost_sum: T::zero(),
2624
}
2725
}
2826

@@ -56,7 +54,6 @@ where
5654
assert!(cost >= T::zero());
5755

5856
self.pos.push((from, self.g[from].len()));
59-
self.cost_sum += cost;
6057

6158
let rev = self.g[to].len();
6259
self.g[from].push(_Edge { to, rev, cap, cost });
@@ -131,7 +128,7 @@ where
131128
pe: &mut [usize],
132129
) -> bool {
133130
let n = self.g.len();
134-
let mut dist = vec![self.cost_sum; n];
131+
let mut dist = vec![T::max_value(); n];
135132
let mut vis = vec![false; n];
136133

137134
let mut que = std::collections::BinaryHeap::new();
@@ -209,4 +206,13 @@ mod tests {
209206
let expected = [(0, 0), (3, 3)];
210207
assert_eq!(expected[..], *graph.slope(0, 2, i32::max_value()));
211208
}
209+
210+
#[test]
211+
fn only_one_nonzero_cost_edge() {
212+
let mut graph = MinCostFlowGraph::new(3);
213+
assert_eq!(0, graph.add_edge(0, 1, 1, 1));
214+
assert_eq!(1, graph.add_edge(1, 2, 1, 0));
215+
let expected = [(0, 0), (1, 1)];
216+
assert_eq!(expected[..], *graph.slope(0, 2, i32::max_value()));
217+
}
212218
}

0 commit comments

Comments
 (0)