Skip to content

Commit 7ac1708

Browse files
authored
Merge pull request #67 from atcoder/patch/mcf
nit picking optimization of mincostflow
2 parents 114e690 + 5ac2409 commit 7ac1708

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

atcoder/mincostflow.hpp

+15-12
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,25 @@ template <class Cap, class Cost> struct mcf_graph {
7272
std::vector<Cost> dual(_n, 0), dist(_n);
7373
std::vector<int> pv(_n), pe(_n);
7474
std::vector<bool> vis(_n);
75+
struct Q {
76+
Cost key;
77+
int to;
78+
bool operator<(Q r) const { return key > r.key; }
79+
};
80+
std::vector<Q> que;
7581
auto dual_ref = [&]() {
7682
std::fill(dist.begin(), dist.end(),
7783
std::numeric_limits<Cost>::max());
78-
std::fill(pv.begin(), pv.end(), -1);
79-
std::fill(pe.begin(), pe.end(), -1);
8084
std::fill(vis.begin(), vis.end(), false);
81-
struct Q {
82-
Cost key;
83-
int to;
84-
bool operator<(Q r) const { return key > r.key; }
85-
};
86-
std::priority_queue<Q> que;
85+
que.clear();
86+
8787
dist[s] = 0;
88-
que.push(Q{0, s});
88+
que.push_back(Q{0, s});
89+
std::push_heap(que.begin(), que.end());
8990
while (!que.empty()) {
90-
int v = que.top().to;
91-
que.pop();
91+
int v = que.front().to;
92+
std::pop_heap(que.begin(), que.end());
93+
que.pop_back();
9294
if (vis[v]) continue;
9395
vis[v] = true;
9496
if (v == t) break;
@@ -105,7 +107,8 @@ template <class Cap, class Cost> struct mcf_graph {
105107
dist[e.to] = dist[v] + cost;
106108
pv[e.to] = v;
107109
pe[e.to] = i;
108-
que.push(Q{dist[e.to], e.to});
110+
que.push_back(Q{dist[e.to], e.to});
111+
std::push_heap(que.begin(), que.end());
109112
}
110113
}
111114
}

0 commit comments

Comments
 (0)