QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#397694 | #2208. Flow | alpha1022 | AC ✓ | 118ms | 4512kb | C++14 | 3.0kb | 2024-04-24 16:01:21 | 2024-04-24 16:01:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template<class Z, class ZC>
struct CostFlow {
int n;
vector<vector<tuple<int, int, Z, ZC>>> g;
CostFlow(int n) : n(n), g(n) {}
void addEdge(int u, int v, Z c, ZC w) {
int ru = g[u].size(), rv = g[v].size();
g[u].emplace_back(v, rv, c, w), g[v].emplace_back(u, ru, 0, -w);
}
pair<Z, ZC> dinic(int s, int t) {
vector<ZC> h(n), dis(n);
vector<int> level(n), inQueue(n), cur(n);
auto getH = [&]() {
fill_n(h.begin(), n, numeric_limits<ZC>::max());
queue<int> q; q.push(s), h[s] = 0, inQueue[s] = 1;
while (!q.empty()) {
int u = q.front(); q.pop(), inQueue[u] = 0;
for (auto [v, _, c, w]: g[u]) {
if (c && h[v] > h[u] + w) {
h[v] = h[u] + w;
if (!inQueue[v]) q.push(v), inQueue[v] = 1;
}
}
}
};
getH();
if (h[t] == numeric_limits<ZC>::max()) return {0, 0};
auto getLevel = [&]() {
fill_n(dis.begin(), n, numeric_limits<ZC>::max());
priority_queue<pair<ZC, int>, vector<pair<ZC, int>>, greater<pair<ZC, int>>> pQ;
pQ.emplace(dis[s] = 0, s);
while (!pQ.empty()) {
auto [d, u] = pQ.top(); pQ.pop();
if (d > dis[u]) continue;
for (auto [v, _, c, w]: g[u]) {
w += h[u] - h[v];
if (c && dis[v] > dis[u] + w) pQ.emplace(dis[v] = dis[u] + w, v);
}
}
queue<int> q; fill_n(level.begin(), n, -1), q.push(s), level[s] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
for (auto [v, _, c, w]: g[u])
if (c && dis[v] == dis[u] + w + h[u] - h[v] && level[v] == -1) level[v] = level[u] + 1, q.push(v);
}
};
Z ret = 0; ZC cost = 0;
function<Z(int, Z)> cap = [&](int u, Z lim) {
if (u == t) return lim;
Z ret = 0;
for (; cur[u] && ret < lim; --cur[u]) {
auto &[v, rev, c, w] = g[u][cur[u] - 1];
if (c && dis[v] == dis[u] + w + h[u] - h[v] && level[v] == level[u] + 1) {
Z flow = cap(v, min(lim - ret, c));
ret += flow, cost += flow * w, c -= flow, get<2>(g[v][rev]) += flow;
if (ret == lim) return ret;
}
}
return ret;
};
while (getLevel(), level[t] != -1) {
for (int i = 0; i < n; ++i) cur[i] = g[i].size();
ret += cap(s, numeric_limits<Z>::max());
for (int i = 0; i < n; ++i) h[i] += dis[i];
}
return {ret, cost};
}
};
int main() {
int n, m; scanf("%d%d", &n, &m);
int ans = 0;
vector<int> sum(n);
CostFlow<int, int> costFlow(n + 2);
int s = n, t = n + 1;
for (int i = 0; i < m; ++i) {
int u, v, c; scanf("%d%d%d", &u, &v, &c), --u, --v;
sum[u] -= c, sum[v] += c, ans += c, costFlow.addEdge(v, u, c, 1);
}
for (int i = 0; i < n; ++i)
if (sum[i] < 0) costFlow.addEdge(i, t, -sum[i], 0);
else if (sum[i] > 0) costFlow.addEdge(s, i, sum[i], 0);
printf("%d\n", ans -= costFlow.dinic(s, t).second);
}
Details
Test #1:
score: 100
Accepted
time: 30ms
memory: 4404kb
Test #2:
score: 0
Accepted
time: 11ms
memory: 4088kb
Test #3:
score: 0
Accepted
time: 22ms
memory: 4172kb
Test #4:
score: 0
Accepted
time: 27ms
memory: 4132kb
Test #5:
score: 0
Accepted
time: 47ms
memory: 4400kb
Test #6:
score: 0
Accepted
time: 42ms
memory: 4136kb
Test #7:
score: 0
Accepted
time: 81ms
memory: 4192kb
Test #8:
score: 0
Accepted
time: 17ms
memory: 4128kb
Test #9:
score: 0
Accepted
time: 26ms
memory: 4080kb
Test #10:
score: 0
Accepted
time: 39ms
memory: 4420kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 3980kb
Test #12:
score: 0
Accepted
time: 40ms
memory: 4220kb
Test #13:
score: 0
Accepted
time: 68ms
memory: 4180kb
Test #14:
score: 0
Accepted
time: 101ms
memory: 4200kb
Test #15:
score: 0
Accepted
time: 5ms
memory: 4064kb
Test #16:
score: 0
Accepted
time: 11ms
memory: 4088kb
Test #17:
score: 0
Accepted
time: 21ms
memory: 4028kb
Test #18:
score: 0
Accepted
time: 5ms
memory: 4308kb
Test #19:
score: 0
Accepted
time: 94ms
memory: 4164kb
Test #20:
score: 0
Accepted
time: 1ms
memory: 3976kb
Test #21:
score: 0
Accepted
time: 37ms
memory: 4428kb
Test #22:
score: 0
Accepted
time: 18ms
memory: 4080kb
Test #23:
score: 0
Accepted
time: 53ms
memory: 4088kb
Test #24:
score: 0
Accepted
time: 36ms
memory: 4424kb
Test #25:
score: 0
Accepted
time: 105ms
memory: 4216kb
Test #26:
score: 0
Accepted
time: 23ms
memory: 4176kb
Test #27:
score: 0
Accepted
time: 6ms
memory: 4112kb
Test #28:
score: 0
Accepted
time: 8ms
memory: 4292kb
Test #29:
score: 0
Accepted
time: 79ms
memory: 4460kb
Test #30:
score: 0
Accepted
time: 85ms
memory: 4480kb
Test #31:
score: 0
Accepted
time: 19ms
memory: 4024kb
Test #32:
score: 0
Accepted
time: 93ms
memory: 4140kb
Test #33:
score: 0
Accepted
time: 40ms
memory: 4132kb
Test #34:
score: 0
Accepted
time: 85ms
memory: 4488kb
Test #35:
score: 0
Accepted
time: 18ms
memory: 4064kb
Test #36:
score: 0
Accepted
time: 70ms
memory: 4476kb
Test #37:
score: 0
Accepted
time: 88ms
memory: 4180kb
Test #38:
score: 0
Accepted
time: 64ms
memory: 4176kb
Test #39:
score: 0
Accepted
time: 37ms
memory: 4120kb
Test #40:
score: 0
Accepted
time: 49ms
memory: 4224kb
Test #41:
score: 0
Accepted
time: 7ms
memory: 4008kb
Test #42:
score: 0
Accepted
time: 11ms
memory: 4080kb
Test #43:
score: 0
Accepted
time: 29ms
memory: 4144kb
Test #44:
score: 0
Accepted
time: 54ms
memory: 4152kb
Test #45:
score: 0
Accepted
time: 70ms
memory: 4136kb
Test #46:
score: 0
Accepted
time: 72ms
memory: 4160kb
Test #47:
score: 0
Accepted
time: 22ms
memory: 4352kb
Test #48:
score: 0
Accepted
time: 62ms
memory: 4452kb
Test #49:
score: 0
Accepted
time: 33ms
memory: 4188kb
Test #50:
score: 0
Accepted
time: 43ms
memory: 4152kb
Test #51:
score: 0
Accepted
time: 11ms
memory: 4148kb
Test #52:
score: 0
Accepted
time: 6ms
memory: 4344kb
Test #53:
score: 0
Accepted
time: 3ms
memory: 4064kb
Test #54:
score: 0
Accepted
time: 96ms
memory: 4204kb
Test #55:
score: 0
Accepted
time: 2ms
memory: 4008kb
Test #56:
score: 0
Accepted
time: 36ms
memory: 4108kb
Test #57:
score: 0
Accepted
time: 11ms
memory: 4380kb
Test #58:
score: 0
Accepted
time: 70ms
memory: 4200kb
Test #59:
score: 0
Accepted
time: 78ms
memory: 4172kb
Test #60:
score: 0
Accepted
time: 71ms
memory: 4188kb
Test #61:
score: 0
Accepted
time: 93ms
memory: 4464kb
Test #62:
score: 0
Accepted
time: 18ms
memory: 4092kb
Test #63:
score: 0
Accepted
time: 25ms
memory: 4104kb
Test #64:
score: 0
Accepted
time: 2ms
memory: 3996kb
Test #65:
score: 0
Accepted
time: 35ms
memory: 4152kb
Test #66:
score: 0
Accepted
time: 81ms
memory: 4480kb
Test #67:
score: 0
Accepted
time: 5ms
memory: 4044kb
Test #68:
score: 0
Accepted
time: 51ms
memory: 4448kb
Test #69:
score: 0
Accepted
time: 26ms
memory: 4396kb
Test #70:
score: 0
Accepted
time: 11ms
memory: 4088kb
Test #71:
score: 0
Accepted
time: 0ms
memory: 4040kb
Test #72:
score: 0
Accepted
time: 9ms
memory: 4372kb
Test #73:
score: 0
Accepted
time: 13ms
memory: 4084kb
Test #74:
score: 0
Accepted
time: 29ms
memory: 4068kb
Test #75:
score: 0
Accepted
time: 12ms
memory: 4364kb
Test #76:
score: 0
Accepted
time: 21ms
memory: 4360kb
Test #77:
score: 0
Accepted
time: 26ms
memory: 4112kb
Test #78:
score: 0
Accepted
time: 97ms
memory: 4220kb
Test #79:
score: 0
Accepted
time: 84ms
memory: 4196kb
Test #80:
score: 0
Accepted
time: 42ms
memory: 4140kb
Test #81:
score: 0
Accepted
time: 18ms
memory: 4108kb
Test #82:
score: 0
Accepted
time: 89ms
memory: 4132kb
Test #83:
score: 0
Accepted
time: 57ms
memory: 4160kb
Test #84:
score: 0
Accepted
time: 44ms
memory: 4152kb
Test #85:
score: 0
Accepted
time: 86ms
memory: 4220kb
Test #86:
score: 0
Accepted
time: 41ms
memory: 4240kb
Test #87:
score: 0
Accepted
time: 0ms
memory: 4028kb
Test #88:
score: 0
Accepted
time: 20ms
memory: 4168kb
Test #89:
score: 0
Accepted
time: 9ms
memory: 4148kb
Test #90:
score: 0
Accepted
time: 101ms
memory: 4204kb
Test #91:
score: 0
Accepted
time: 14ms
memory: 4380kb
Test #92:
score: 0
Accepted
time: 1ms
memory: 4056kb
Test #93:
score: 0
Accepted
time: 23ms
memory: 4164kb
Test #94:
score: 0
Accepted
time: 59ms
memory: 4440kb
Test #95:
score: 0
Accepted
time: 16ms
memory: 4028kb
Test #96:
score: 0
Accepted
time: 4ms
memory: 4000kb
Test #97:
score: 0
Accepted
time: 23ms
memory: 4396kb
Test #98:
score: 0
Accepted
time: 88ms
memory: 4196kb
Test #99:
score: 0
Accepted
time: 2ms
memory: 4076kb
Test #100:
score: 0
Accepted
time: 91ms
memory: 4216kb
Test #101:
score: 0
Accepted
time: 102ms
memory: 4288kb
Test #102:
score: 0
Accepted
time: 115ms
memory: 4508kb
Test #103:
score: 0
Accepted
time: 2ms
memory: 4040kb
Test #104:
score: 0
Accepted
time: 114ms
memory: 4216kb
Test #105:
score: 0
Accepted
time: 30ms
memory: 4164kb
Test #106:
score: 0
Accepted
time: 53ms
memory: 4404kb
Test #107:
score: 0
Accepted
time: 9ms
memory: 4060kb
Test #108:
score: 0
Accepted
time: 29ms
memory: 4396kb
Test #109:
score: 0
Accepted
time: 13ms
memory: 4164kb
Test #110:
score: 0
Accepted
time: 25ms
memory: 4128kb
Test #111:
score: 0
Accepted
time: 16ms
memory: 4376kb
Test #112:
score: 0
Accepted
time: 18ms
memory: 4096kb
Test #113:
score: 0
Accepted
time: 45ms
memory: 4136kb
Test #114:
score: 0
Accepted
time: 55ms
memory: 4440kb
Test #115:
score: 0
Accepted
time: 48ms
memory: 4136kb
Test #116:
score: 0
Accepted
time: 5ms
memory: 4056kb
Test #117:
score: 0
Accepted
time: 17ms
memory: 4056kb
Test #118:
score: 0
Accepted
time: 16ms
memory: 4088kb
Test #119:
score: 0
Accepted
time: 34ms
memory: 4440kb
Test #120:
score: 0
Accepted
time: 35ms
memory: 4104kb
Test #121:
score: 0
Accepted
time: 73ms
memory: 4488kb
Test #122:
score: 0
Accepted
time: 63ms
memory: 4192kb
Test #123:
score: 0
Accepted
time: 30ms
memory: 4104kb
Test #124:
score: 0
Accepted
time: 79ms
memory: 4204kb
Test #125:
score: 0
Accepted
time: 98ms
memory: 4184kb
Test #126:
score: 0
Accepted
time: 28ms
memory: 4088kb
Test #127:
score: 0
Accepted
time: 1ms
memory: 3952kb
Test #128:
score: 0
Accepted
time: 70ms
memory: 4472kb
Test #129:
score: 0
Accepted
time: 49ms
memory: 4092kb
Test #130:
score: 0
Accepted
time: 3ms
memory: 4056kb
Test #131:
score: 0
Accepted
time: 31ms
memory: 4388kb
Test #132:
score: 0
Accepted
time: 8ms
memory: 4080kb
Test #133:
score: 0
Accepted
time: 76ms
memory: 4176kb
Test #134:
score: 0
Accepted
time: 85ms
memory: 4152kb
Test #135:
score: 0
Accepted
time: 65ms
memory: 4172kb
Test #136:
score: 0
Accepted
time: 28ms
memory: 4136kb
Test #137:
score: 0
Accepted
time: 22ms
memory: 4168kb
Test #138:
score: 0
Accepted
time: 22ms
memory: 4408kb
Test #139:
score: 0
Accepted
time: 24ms
memory: 4080kb
Test #140:
score: 0
Accepted
time: 73ms
memory: 4180kb
Test #141:
score: 0
Accepted
time: 80ms
memory: 4200kb
Test #142:
score: 0
Accepted
time: 63ms
memory: 4408kb
Test #143:
score: 0
Accepted
time: 78ms
memory: 4180kb
Test #144:
score: 0
Accepted
time: 71ms
memory: 4240kb
Test #145:
score: 0
Accepted
time: 22ms
memory: 4352kb
Test #146:
score: 0
Accepted
time: 20ms
memory: 4036kb
Test #147:
score: 0
Accepted
time: 37ms
memory: 4428kb
Test #148:
score: 0
Accepted
time: 39ms
memory: 4136kb
Test #149:
score: 0
Accepted
time: 84ms
memory: 4484kb
Test #150:
score: 0
Accepted
time: 6ms
memory: 4044kb
Test #151:
score: 0
Accepted
time: 33ms
memory: 4208kb
Test #152:
score: 0
Accepted
time: 24ms
memory: 4124kb
Test #153:
score: 0
Accepted
time: 93ms
memory: 4212kb
Test #154:
score: 0
Accepted
time: 20ms
memory: 4364kb
Test #155:
score: 0
Accepted
time: 51ms
memory: 4144kb
Test #156:
score: 0
Accepted
time: 100ms
memory: 4204kb
Test #157:
score: 0
Accepted
time: 6ms
memory: 4124kb
Test #158:
score: 0
Accepted
time: 64ms
memory: 4436kb
Test #159:
score: 0
Accepted
time: 94ms
memory: 4248kb
Test #160:
score: 0
Accepted
time: 13ms
memory: 4096kb
Test #161:
score: 0
Accepted
time: 10ms
memory: 4088kb
Test #162:
score: 0
Accepted
time: 8ms
memory: 4036kb
Test #163:
score: 0
Accepted
time: 8ms
memory: 4360kb
Test #164:
score: 0
Accepted
time: 37ms
memory: 4400kb
Test #165:
score: 0
Accepted
time: 8ms
memory: 4076kb
Test #166:
score: 0
Accepted
time: 53ms
memory: 4116kb
Test #167:
score: 0
Accepted
time: 39ms
memory: 4172kb
Test #168:
score: 0
Accepted
time: 99ms
memory: 4224kb
Test #169:
score: 0
Accepted
time: 99ms
memory: 4220kb
Test #170:
score: 0
Accepted
time: 34ms
memory: 4096kb
Test #171:
score: 0
Accepted
time: 64ms
memory: 4436kb
Test #172:
score: 0
Accepted
time: 13ms
memory: 4128kb
Test #173:
score: 0
Accepted
time: 72ms
memory: 4460kb
Test #174:
score: 0
Accepted
time: 17ms
memory: 4088kb
Test #175:
score: 0
Accepted
time: 26ms
memory: 4392kb
Test #176:
score: 0
Accepted
time: 79ms
memory: 4156kb
Test #177:
score: 0
Accepted
time: 12ms
memory: 4376kb
Test #178:
score: 0
Accepted
time: 23ms
memory: 4096kb
Test #179:
score: 0
Accepted
time: 33ms
memory: 4408kb
Test #180:
score: 0
Accepted
time: 3ms
memory: 4012kb
Test #181:
score: 0
Accepted
time: 58ms
memory: 4456kb
Test #182:
score: 0
Accepted
time: 14ms
memory: 4084kb
Test #183:
score: 0
Accepted
time: 14ms
memory: 4088kb
Test #184:
score: 0
Accepted
time: 118ms
memory: 4512kb
Test #185:
score: 0
Accepted
time: 108ms
memory: 4220kb
Test #186:
score: 0
Accepted
time: 110ms
memory: 4208kb