Skip to content

Commit 1ca9100

Browse files
committed
fix issue #1
1 parent 54f7f17 commit 1ca9100

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

Diff for: atcoder/maxflow.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ template <class Cap> struct mf_graph {
2121
assert(0 <= cap);
2222
int m = int(pos.size());
2323
pos.push_back({from, int(g[from].size())});
24-
g[from].push_back(_edge{to, int(g[to].size()), cap});
25-
g[to].push_back(_edge{from, int(g[from].size()) - 1, 0});
24+
int from_id = int(g[from].size());
25+
int to_id = int(g[to].size());
26+
if (from == to) to_id++;
27+
g[from].push_back(_edge{to, to_id, cap});
28+
g[to].push_back(_edge{from, from_id, 0});
2629
return m;
2730
}
2831

Diff for: atcoder/mincostflow.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ template <class Cap, class Cost> struct mcf_graph {
1919
assert(0 <= to && to < _n);
2020
int m = int(pos.size());
2121
pos.push_back({from, int(g[from].size())});
22-
g[from].push_back(_edge{to, int(g[to].size()), cap, cost});
23-
g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost});
22+
int from_id = int(g[from].size());
23+
int to_id = int(g[to].size());
24+
if (from == to) to_id++;
25+
g[from].push_back(_edge{to, to_id, cap, cost});
26+
g[to].push_back(_edge{from, from_id, 0, -cost});
2427
return m;
2528
}
2629

Diff for: test/unittest/maxflow_test.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,12 @@ TEST(MaxflowTest, BoundUint) {
169169
e = {0, 2, INF, INF};
170170
edge_eq(e, g.get_edge(2));
171171
}
172+
173+
// https://github.com/atcoder/ac-library/issues/1
174+
TEST(MaxflowTest, SelfLoop) {
175+
mf_graph<int> g(3);
176+
ASSERT_EQ(0, g.add_edge(0, 0, 100));
177+
178+
mf_graph<int>::edge e = {0, 0, 100, 0};
179+
edge_eq(e, g.get_edge(0));
180+
}

Diff for: test/unittest/mincostflow_test.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,12 @@ TEST(MincostflowTest, OutOfRange) {
7070
EXPECT_DEATH(g.slope(-1, 3), ".*");
7171
EXPECT_DEATH(g.slope(3, 3), ".*");
7272
}
73+
74+
// https://github.com/atcoder/ac-library/issues/1
75+
TEST(MaxflowTest, SelfLoop) {
76+
mcf_graph<int, int> g(3);
77+
ASSERT_EQ(0, g.add_edge(0, 0, 100, 123));
78+
79+
mcf_graph<int, int>::edge e = {0, 0, 100, 0, 123};
80+
edge_eq(e, g.get_edge(0));
81+
}

0 commit comments

Comments
 (0)