QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#401911 | #2208. Flow | iee | AC ✓ | 1481ms | 4320kb | C++14 | 1.6kb | 2024-04-29 16:34:42 | 2024-04-29 16:34:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
int S, T;
constexpr int N = 4000 + 5, inf = 1e18;
struct MCF {
struct edge { int v, f, c; };
vector<edge> l;
vector<int> e[N];
void add(int u, int v, int f, int c) {
e[u].push_back(l.size());
l.push_back({v, f, c});
e[v].push_back(l.size());
l.push_back({u, 0, -c});
}
int dis[N], h[N], lim[N];
array<int, 2> pre[N];
bool dij() {
fill(dis, dis + N, inf), fill(lim, lim + N, inf);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;
q.push({dis[S] = 0, S});
while (!q.empty()) {
auto [d, u] = q.top();
q.pop();
if (d != dis[u]) continue;
for (int i : e[u]) {
auto [v, f, c] = l[i];
if (f && dis[v] > dis[u] + h[u] - h[v] + c) {
dis[v] = dis[u] + h[u] - h[v] + c;
lim[v] = min(lim[u], f);
pre[v] = {u, i};
q.push({dis[v], v});
}
}
}
for (int i = 0; i < N; i++) {
h[i] = min(inf, h[i] + dis[i]);
}
return dis[T] < 2e9;
}
int flow() {
int res = 0;
while (dij()) {
int fl = lim[T];
for (int v = T; v != S; ) {
auto [u, i] = pre[v];
l[i].f -= fl;
l[i ^ 1].f += fl;
v = u;
}
res += fl * h[T];
}
return res;
}
} g;
int n, m;
int sum[N];
signed main() {
cin >> n >> m;
int res = 0;
for (int i = 1, u, v, w; i <= m; i++) {
cin >> u >> v >> w;
g.add(v, u, w, 1);
sum[v] += w, sum[u] -= w;
res += w;
}
S = n + 1, T = n + 2;
for (int i = 1; i <= n; i++) {
if (sum[i] > 0) {
g.add(S, i, sum[i], 0);
} else {
g.add(i, T, -sum[i], 0);
}
}
cout << res - g.flow() << "\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 632ms
memory: 3968kb
Test #2:
score: 0
Accepted
time: 297ms
memory: 4240kb
Test #3:
score: 0
Accepted
time: 619ms
memory: 4208kb
Test #4:
score: 0
Accepted
time: 693ms
memory: 3948kb
Test #5:
score: 0
Accepted
time: 932ms
memory: 4268kb
Test #6:
score: 0
Accepted
time: 872ms
memory: 4016kb
Test #7:
score: 0
Accepted
time: 1158ms
memory: 4064kb
Test #8:
score: 0
Accepted
time: 351ms
memory: 4008kb
Test #9:
score: 0
Accepted
time: 636ms
memory: 4180kb
Test #10:
score: 0
Accepted
time: 720ms
memory: 4016kb
Test #11:
score: 0
Accepted
time: 5ms
memory: 3976kb
Test #12:
score: 0
Accepted
time: 846ms
memory: 4012kb
Test #13:
score: 0
Accepted
time: 1179ms
memory: 3996kb
Test #14:
score: 0
Accepted
time: 1368ms
memory: 4008kb
Test #15:
score: 0
Accepted
time: 105ms
memory: 3932kb
Test #16:
score: 0
Accepted
time: 275ms
memory: 3980kb
Test #17:
score: 0
Accepted
time: 522ms
memory: 4020kb
Test #18:
score: 0
Accepted
time: 141ms
memory: 4000kb
Test #19:
score: 0
Accepted
time: 1355ms
memory: 4312kb
Test #20:
score: 0
Accepted
time: 2ms
memory: 3952kb
Test #21:
score: 0
Accepted
time: 776ms
memory: 4028kb
Test #22:
score: 0
Accepted
time: 395ms
memory: 4240kb
Test #23:
score: 0
Accepted
time: 897ms
memory: 4040kb
Test #24:
score: 0
Accepted
time: 751ms
memory: 4040kb
Test #25:
score: 0
Accepted
time: 1424ms
memory: 4044kb
Test #26:
score: 0
Accepted
time: 707ms
memory: 4016kb
Test #27:
score: 0
Accepted
time: 154ms
memory: 3888kb
Test #28:
score: 0
Accepted
time: 165ms
memory: 4004kb
Test #29:
score: 0
Accepted
time: 1101ms
memory: 4276kb
Test #30:
score: 0
Accepted
time: 1298ms
memory: 4056kb
Test #31:
score: 0
Accepted
time: 430ms
memory: 3956kb
Test #32:
score: 0
Accepted
time: 1295ms
memory: 4052kb
Test #33:
score: 0
Accepted
time: 778ms
memory: 4016kb
Test #34:
score: 0
Accepted
time: 1292ms
memory: 4096kb
Test #35:
score: 0
Accepted
time: 499ms
memory: 4008kb
Test #36:
score: 0
Accepted
time: 1173ms
memory: 4024kb
Test #37:
score: 0
Accepted
time: 1329ms
memory: 4068kb
Test #38:
score: 0
Accepted
time: 1105ms
memory: 4036kb
Test #39:
score: 0
Accepted
time: 681ms
memory: 4016kb
Test #40:
score: 0
Accepted
time: 1023ms
memory: 3996kb
Test #41:
score: 0
Accepted
time: 173ms
memory: 4008kb
Test #42:
score: 0
Accepted
time: 251ms
memory: 3996kb
Test #43:
score: 0
Accepted
time: 661ms
memory: 3896kb
Test #44:
score: 0
Accepted
time: 1064ms
memory: 4260kb
Test #45:
score: 0
Accepted
time: 1239ms
memory: 4052kb
Test #46:
score: 0
Accepted
time: 996ms
memory: 4020kb
Test #47:
score: 0
Accepted
time: 494ms
memory: 4248kb
Test #48:
score: 0
Accepted
time: 1102ms
memory: 3932kb
Test #49:
score: 0
Accepted
time: 608ms
memory: 3968kb
Test #50:
score: 0
Accepted
time: 920ms
memory: 4028kb
Test #51:
score: 0
Accepted
time: 293ms
memory: 4004kb
Test #52:
score: 0
Accepted
time: 96ms
memory: 4228kb
Test #53:
score: 0
Accepted
time: 77ms
memory: 4192kb
Test #54:
score: 0
Accepted
time: 1375ms
memory: 4268kb
Test #55:
score: 0
Accepted
time: 39ms
memory: 3988kb
Test #56:
score: 0
Accepted
time: 780ms
memory: 4220kb
Test #57:
score: 0
Accepted
time: 303ms
memory: 3980kb
Test #58:
score: 0
Accepted
time: 1266ms
memory: 3988kb
Test #59:
score: 0
Accepted
time: 1201ms
memory: 4304kb
Test #60:
score: 0
Accepted
time: 1180ms
memory: 4292kb
Test #61:
score: 0
Accepted
time: 1327ms
memory: 4072kb
Test #62:
score: 0
Accepted
time: 322ms
memory: 3984kb
Test #63:
score: 0
Accepted
time: 653ms
memory: 3980kb
Test #64:
score: 0
Accepted
time: 108ms
memory: 4196kb
Test #65:
score: 0
Accepted
time: 770ms
memory: 4248kb
Test #66:
score: 0
Accepted
time: 1255ms
memory: 4256kb
Test #67:
score: 0
Accepted
time: 144ms
memory: 4232kb
Test #68:
score: 0
Accepted
time: 1008ms
memory: 4284kb
Test #69:
score: 0
Accepted
time: 624ms
memory: 3992kb
Test #70:
score: 0
Accepted
time: 353ms
memory: 4204kb
Test #71:
score: 0
Accepted
time: 22ms
memory: 3940kb
Test #72:
score: 0
Accepted
time: 248ms
memory: 3948kb
Test #73:
score: 0
Accepted
time: 415ms
memory: 3896kb
Test #74:
score: 0
Accepted
time: 725ms
memory: 4012kb
Test #75:
score: 0
Accepted
time: 248ms
memory: 3888kb
Test #76:
score: 0
Accepted
time: 553ms
memory: 3944kb
Test #77:
score: 0
Accepted
time: 698ms
memory: 4000kb
Test #78:
score: 0
Accepted
time: 1413ms
memory: 4076kb
Test #79:
score: 0
Accepted
time: 1267ms
memory: 4228kb
Test #80:
score: 0
Accepted
time: 824ms
memory: 3904kb
Test #81:
score: 0
Accepted
time: 467ms
memory: 4008kb
Test #82:
score: 0
Accepted
time: 1270ms
memory: 3988kb
Test #83:
score: 0
Accepted
time: 1019ms
memory: 4052kb
Test #84:
score: 0
Accepted
time: 781ms
memory: 4188kb
Test #85:
score: 0
Accepted
time: 1400ms
memory: 4100kb
Test #86:
score: 0
Accepted
time: 1056ms
memory: 4024kb
Test #87:
score: 0
Accepted
time: 15ms
memory: 4180kb
Test #88:
score: 0
Accepted
time: 438ms
memory: 4016kb
Test #89:
score: 0
Accepted
time: 263ms
memory: 3976kb
Test #90:
score: 0
Accepted
time: 1329ms
memory: 4320kb
Test #91:
score: 0
Accepted
time: 461ms
memory: 3956kb
Test #92:
score: 0
Accepted
time: 35ms
memory: 3996kb
Test #93:
score: 0
Accepted
time: 554ms
memory: 3948kb
Test #94:
score: 0
Accepted
time: 1053ms
memory: 3968kb
Test #95:
score: 0
Accepted
time: 421ms
memory: 4040kb
Test #96:
score: 0
Accepted
time: 104ms
memory: 3932kb
Test #97:
score: 0
Accepted
time: 606ms
memory: 4008kb
Test #98:
score: 0
Accepted
time: 1304ms
memory: 4232kb
Test #99:
score: 0
Accepted
time: 39ms
memory: 3988kb
Test #100:
score: 0
Accepted
time: 1452ms
memory: 4236kb
Test #101:
score: 0
Accepted
time: 1369ms
memory: 4064kb
Test #102:
score: 0
Accepted
time: 1450ms
memory: 4080kb
Test #103:
score: 0
Accepted
time: 187ms
memory: 3988kb
Test #104:
score: 0
Accepted
time: 1404ms
memory: 4316kb
Test #105:
score: 0
Accepted
time: 586ms
memory: 4008kb
Test #106:
score: 0
Accepted
time: 923ms
memory: 4248kb
Test #107:
score: 0
Accepted
time: 294ms
memory: 4012kb
Test #108:
score: 0
Accepted
time: 639ms
memory: 3964kb
Test #109:
score: 0
Accepted
time: 293ms
memory: 4004kb
Test #110:
score: 0
Accepted
time: 688ms
memory: 3900kb
Test #111:
score: 0
Accepted
time: 403ms
memory: 4256kb
Test #112:
score: 0
Accepted
time: 445ms
memory: 4196kb
Test #113:
score: 0
Accepted
time: 783ms
memory: 3964kb
Test #114:
score: 0
Accepted
time: 988ms
memory: 3960kb
Test #115:
score: 0
Accepted
time: 797ms
memory: 4260kb
Test #116:
score: 0
Accepted
time: 102ms
memory: 4004kb
Test #117:
score: 0
Accepted
time: 486ms
memory: 4008kb
Test #118:
score: 0
Accepted
time: 403ms
memory: 3940kb
Test #119:
score: 0
Accepted
time: 866ms
memory: 3992kb
Test #120:
score: 0
Accepted
time: 719ms
memory: 4184kb
Test #121:
score: 0
Accepted
time: 1305ms
memory: 4304kb
Test #122:
score: 0
Accepted
time: 985ms
memory: 4024kb
Test #123:
score: 0
Accepted
time: 593ms
memory: 3900kb
Test #124:
score: 0
Accepted
time: 1343ms
memory: 4000kb
Test #125:
score: 0
Accepted
time: 1385ms
memory: 4300kb
Test #126:
score: 0
Accepted
time: 542ms
memory: 4012kb
Test #127:
score: 0
Accepted
time: 6ms
memory: 3940kb
Test #128:
score: 0
Accepted
time: 1214ms
memory: 4064kb
Test #129:
score: 0
Accepted
time: 866ms
memory: 3968kb
Test #130:
score: 0
Accepted
time: 64ms
memory: 3996kb
Test #131:
score: 0
Accepted
time: 585ms
memory: 3956kb
Test #132:
score: 0
Accepted
time: 198ms
memory: 4168kb
Test #133:
score: 0
Accepted
time: 1338ms
memory: 4076kb
Test #134:
score: 0
Accepted
time: 1454ms
memory: 3956kb
Test #135:
score: 0
Accepted
time: 1078ms
memory: 4032kb
Test #136:
score: 0
Accepted
time: 760ms
memory: 3992kb
Test #137:
score: 0
Accepted
time: 600ms
memory: 3980kb
Test #138:
score: 0
Accepted
time: 552ms
memory: 4008kb
Test #139:
score: 0
Accepted
time: 525ms
memory: 4020kb
Test #140:
score: 0
Accepted
time: 1075ms
memory: 4292kb
Test #141:
score: 0
Accepted
time: 1364ms
memory: 4100kb
Test #142:
score: 0
Accepted
time: 996ms
memory: 3908kb
Test #143:
score: 0
Accepted
time: 1206ms
memory: 4000kb
Test #144:
score: 0
Accepted
time: 1164ms
memory: 4024kb
Test #145:
score: 0
Accepted
time: 462ms
memory: 3892kb
Test #146:
score: 0
Accepted
time: 500ms
memory: 3960kb
Test #147:
score: 0
Accepted
time: 888ms
memory: 3852kb
Test #148:
score: 0
Accepted
time: 783ms
memory: 4020kb
Test #149:
score: 0
Accepted
time: 1322ms
memory: 4304kb
Test #150:
score: 0
Accepted
time: 142ms
memory: 4008kb
Test #151:
score: 0
Accepted
time: 728ms
memory: 4016kb
Test #152:
score: 0
Accepted
time: 436ms
memory: 4044kb
Test #153:
score: 0
Accepted
time: 1299ms
memory: 4228kb
Test #154:
score: 0
Accepted
time: 571ms
memory: 4048kb
Test #155:
score: 0
Accepted
time: 917ms
memory: 3908kb
Test #156:
score: 0
Accepted
time: 1436ms
memory: 4076kb
Test #157:
score: 0
Accepted
time: 203ms
memory: 3996kb
Test #158:
score: 0
Accepted
time: 946ms
memory: 4016kb
Test #159:
score: 0
Accepted
time: 1341ms
memory: 4012kb
Test #160:
score: 0
Accepted
time: 345ms
memory: 4048kb
Test #161:
score: 0
Accepted
time: 300ms
memory: 3980kb
Test #162:
score: 0
Accepted
time: 188ms
memory: 4008kb
Test #163:
score: 0
Accepted
time: 229ms
memory: 4040kb
Test #164:
score: 0
Accepted
time: 670ms
memory: 3956kb
Test #165:
score: 0
Accepted
time: 178ms
memory: 4228kb
Test #166:
score: 0
Accepted
time: 952ms
memory: 4276kb
Test #167:
score: 0
Accepted
time: 950ms
memory: 4032kb
Test #168:
score: 0
Accepted
time: 1445ms
memory: 3952kb
Test #169:
score: 0
Accepted
time: 1411ms
memory: 3952kb
Test #170:
score: 0
Accepted
time: 564ms
memory: 3956kb
Test #171:
score: 0
Accepted
time: 943ms
memory: 4212kb
Test #172:
score: 0
Accepted
time: 284ms
memory: 3940kb
Test #173:
score: 0
Accepted
time: 1229ms
memory: 3936kb
Test #174:
score: 0
Accepted
time: 427ms
memory: 4008kb
Test #175:
score: 0
Accepted
time: 598ms
memory: 4016kb
Test #176:
score: 0
Accepted
time: 1180ms
memory: 3972kb
Test #177:
score: 0
Accepted
time: 284ms
memory: 3944kb
Test #178:
score: 0
Accepted
time: 430ms
memory: 4044kb
Test #179:
score: 0
Accepted
time: 664ms
memory: 4252kb
Test #180:
score: 0
Accepted
time: 81ms
memory: 3988kb
Test #181:
score: 0
Accepted
time: 1081ms
memory: 3932kb
Test #182:
score: 0
Accepted
time: 448ms
memory: 4016kb
Test #183:
score: 0
Accepted
time: 430ms
memory: 3876kb
Test #184:
score: 0
Accepted
time: 1470ms
memory: 4024kb
Test #185:
score: 0
Accepted
time: 1481ms
memory: 4068kb
Test #186:
score: 0
Accepted
time: 1467ms
memory: 4084kb