QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#282391 | #3249. 分组作业 | kilo_tobo_tarjen# | AC ✓ | 76ms | 23448kb | C++20 | 6.8kb | 2023-12-11 22:02:50 | 2023-12-11 22:02:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const char el = '\n';
typedef long long ll;
typedef long long flow_t;
const flow_t inf_flow = 1e18;
const int inf_dep = 1e9;
struct FlowEdge {
int from, to;
flow_t cap, low = 0, flow = 0;
};
int num_node(const std::vector<FlowEdge> &edges) {
int n = 0;
for (const auto &e : edges) n = std::max({n, e.from, e.to});
return n;
}
flow_t get_flow(const std::vector<FlowEdge> &edges, int s) {
flow_t flow = 0;
for (const auto &e : edges) {
if (e.from == s) flow += e.flow;
}
return flow;
}
struct MaxFlow {
struct Edge {
int from, to;
flow_t cap;
};
int n;
std::vector<std::vector<int>> eid;
std::vector<Edge> edge;
void build(const std::vector<FlowEdge> &edges) {
n = num_node(edges);
eid.assign(n + 1, {});
edge.clear();
int num_edges = 0;
for (const auto &e : edges) {
eid[e.from].push_back(num_edges++);
edge.push_back({e.from, e.to, e.cap - e.flow});
eid[e.to].push_back(num_edges++);
edge.push_back({e.to, e.from, e.flow});
}
}
std::vector<int> dis;
std::vector<int> cur;
bool bfs(int s, int t) {
if (s > n || t > n) return false;
dis.assign(n + 1, inf_dep);
cur.assign(n + 1, 0);
std::queue<int> que;
dis[s] = 0;
que.push(s);
while (que.size()) {
int u = que.front();
que.pop();
for (auto i : eid[u]) {
const auto &e = edge[i];
if (e.cap && dis[e.to] > dis[u] + 1) {
dis[e.to] = dis[u] + 1;
que.push(e.to);
}
}
}
return dis[t] < inf_dep;
}
flow_t dfs(int s, int t, flow_t flim) {
if (s == t) return flim;
flow_t flow = 0;
for (int &i = cur[s]; i < eid[s].size() && flow < flim; i++) {
auto &e = edge[eid[s][i]];
if (dis[e.to] == dis[s] + 1 && e.cap) {
auto detf = dfs(e.to, t, std::min(flim - flow, e.cap));
flow += detf;
e.cap -= detf;
edge[eid[s][i] ^ 1].cap += detf;
}
if (flow == flim) break;
}
return flow;
}
flow_t maxflow(int s, int t) {
flow_t flow = 0;
while (bfs(s, t)) {
flow += dfs(s, t, inf_flow);
}
return flow;
}
std::vector<FlowEdge> to_edge() {
std::vector<FlowEdge> edges;
for (int i = 0; i < edge.size(); i += 2)
edges.push_back({
.from = edge[i].from,
.to = edge[i].to,
.cap = edge[i].cap + edge[i ^ 1].cap,
.low = 0,
.flow = edge[i ^ 1].cap,
});
return edges;
}
};
struct Processor {
std::vector<bool> neg;
std::vector<flow_t> low;
std::vector<flow_t> excess;
void init(std::vector<FlowEdge> &edges) {
int n = num_node(edges);
neg.clear();
neg.reserve(edges.size());
low.clear();
low.reserve(edges.size());
excess.assign(n + 1, 0);
}
void rmv_low(std::vector<FlowEdge> &edges) {
for (auto &e : edges) {
low.push_back(e.low);
if (e.flow >= e.low) {
e.flow -= e.low;
} else {
excess[e.from] -= e.low - e.flow;
excess[e.to] += e.low - e.flow;
e.flow = 0;
}
e.cap -= e.low;
e.low = 0;
}
}
void add_low(std::vector<FlowEdge> &edges) {
reverse(low.begin(), low.end());
for (auto &e : edges) {
e.low = low.back();
e.flow += e.low;
e.cap += e.low;
low.pop_back();
}
}
};
bool excess_flow(std::vector<FlowEdge> &edges,
const std::vector<flow_t> &excess) {
int n = num_node(edges), m = edges.size();
for (int i = 1; i <= n; i++) {
if (excess[i] > 0)
edges.push_back({.from = n + 1, .to = i, .cap = excess[i]});
if (excess[i] < 0)
edges.push_back({.from = i, .to = n + 2, .cap = -excess[i]});
}
MaxFlow g;
g.build(edges);
g.maxflow(n + 1, n + 2);
edges = g.to_edge();
for (int i = m; i < edges.size(); i++)
if (edges[i].flow != edges[i].cap) return false;
edges.resize(m);
return true;
}
std::optional<flow_t> feasible_flow(std::vector<FlowEdge> &edges, int s = 0,
int t = 0) {
if (s && t) edges.push_back({.from = t, .to = s, .cap = inf_flow});
Processor p;
p.init(edges);
p.rmv_low(edges);
if (!excess_flow(edges, p.excess)) return std::nullopt;
if (s && t) edges.pop_back();
p.add_low(edges);
return get_flow(edges, s);
}
std::optional<flow_t> maximum_flow(std::vector<FlowEdge> &edges, int s, int t) {
edges.push_back({.from = t, .to = s, .cap = inf_flow});
Processor p;
p.init(edges);
p.rmv_low(edges);
if (!excess_flow(edges, p.excess)) return std::nullopt;
edges.pop_back();
MaxFlow g;
g.build(edges);
g.maxflow(s, t);
edges = g.to_edge();
p.add_low(edges);
return get_flow(edges, s);
}
std::optional<flow_t> minimum_flow(std::vector<FlowEdge> &edges, int s, int t) {
edges.push_back({.from = t, .to = s, .cap = inf_flow});
Processor p;
p.init(edges);
p.rmv_low(edges);
if (!excess_flow(edges, p.excess)) return std::nullopt;
edges.pop_back();
MaxFlow g;
Processor q;
excess_flow(edges, q.excess);
g.build(edges);
g.maxflow(t, s);
edges = g.to_edge();
p.add_low(edges);
return get_flow(edges, s);
}
const ll inf = 1e13;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(15);
int n, m;
cin >> n >> m;
vector<FlowEdge> ed;
int s = 1, t = 2;
for (int i = 1; i <= n; i++) {
int c1, d1, e1;
int c2, d2, e2;
cin >> c1 >> d1 >> e1 >> c2 >> d2 >> e2;
ed.push_back({.from = s, .to = 6 * i + 1, .cap = inf + d1});
ed.push_back({.from = 6 * i + 1, .to = 6 * i + 4, .cap = inf_flow});
ed.push_back({.from = 6 * i + 4, .to = t, .cap = inf + c1});
ed.push_back({.from = s, .to = 6 * i + 2, .cap = inf});
ed.push_back({.from = 6 * i + 2, .to = 6 * i + 5, .cap = inf_flow});
ed.push_back({.from = 6 * i + 5, .to = t, .cap = inf});
ed.push_back({.from = s, .to = 6 * i + 3, .cap = inf + d2});
ed.push_back({.from = 6 * i + 3, .to = 6 * i + 6, .cap = inf_flow});
ed.push_back({.from = 6 * i + 6, .to = t, .cap = inf + c2});
ed.push_back({.from = 6 * i + 2, .to = 6 * i + 1, .cap = inf_flow});
ed.push_back({.from = 6 * i + 2, .to = 6 * i + 3, .cap = inf_flow});
ed.push_back({.from = 6 * i + 1, .to = 6 * i + 6, .cap = e1});
ed.push_back({.from = 6 * i + 3, .to = 6 * i + 4, .cap = e2});
}
while (m--) {
int a, b, ai, bi;
cin >> a >> b >> ai >> bi;
int xa = (a + 1) / 2, ya = !(a & 1), xb = (b + 1) / 2, yb = !(b & 1);
ed.push_back({.from = 6 * xb + (yb ? 3 : 1), .to = 6 * xa + 5, .cap = ai});
ed.push_back({.from = 6 * xb + 2, .to = 6 * xa + (ya ? 6 : 4), .cap = bi});
}
cout << *maximum_flow(ed, s, t) - 3 * n * inf << el;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 74ms
memory: 23304kb
input:
5000 10000 23060775 12 2 255978380 28 517 5 6624 26 151149 45131806 23849036 489 484971 24970 162846 1993316 188305 56311199 2003 211 1 50534913 517527 364913 882765 298 71 26 122914059 13 65459 18150033 20 607 8 380059068 3873712 228 9813 5449 6370 3309369 37410691 8 181 1 62340851 1705 4 107 8 209...
output:
22929674417
result:
ok single line: '22929674417'
Test #2:
score: 0
Accepted
time: 76ms
memory: 21600kb
input:
5000 10000 10055 16 122 4784525 16 23548777 75 3 412576 26487664 16119952 1092 3168 28 16059 33 4 13 2 1319671 7150391 17548 31559813 3201 6910 499901569 2 86633 8794940 2 4 2 85 1749 9908314 45526 10 631569 2347 18 141185 145333 23 27 117121 3825480 32645 5236 3 32022 1298 8 51750221 233 4 16102047...
output:
21306827991
result:
ok single line: '21306827991'
Test #3:
score: 0
Accepted
time: 74ms
memory: 23160kb
input:
5000 10000 48362 83079 12461784 16 4689773 2 763 3650 1128 2118 75447925 253189 47745409 622140 70841 302 162849091 1258 3399198 91 808632 16168406 10380 2 370511 10 3193109 261594 4 2 128 106331221 2 605 28343 601 19 1224480 37551173 49 78375152 493909 110536 1 836 28790 8 133 8 40 4 533035 879 391...
output:
22066314160
result:
ok single line: '22066314160'
Test #4:
score: 0
Accepted
time: 59ms
memory: 20804kb
input:
5000 10000 5564 607330 2584640 126520704 169669 3 231555 402303 114 2 128 58 290016 13 74249 8126414 279 974 1304 119651095 35466664 992290 3414 63 23564 1091 18168 418 125135735 3 29 1295683 424396930 1993 12647005 20 7 712237 1086773 500004515 6 355786 383393 486213 73 21141 29726 665 1 59959 2020...
output:
22438919820
result:
ok single line: '22438919820'
Test #5:
score: 0
Accepted
time: 70ms
memory: 23448kb
input:
5000 10000 23 1 217 41 249931 61567 3096055 3 7 24 12529 1 246322439 144141318 223606 39 906 2 22654307 3963932 3447414 7949665 51935780 344666 30 5058423 2825 148134 7532713 1140 242 5560395 4264 62940 8262918 182 7825 9865191 2992 138038614 24828018 33318812 11 1 4741355 241 533 3 4337359 741573 3...
output:
21645286114
result:
ok single line: '21645286114'
Test #6:
score: 0
Accepted
time: 63ms
memory: 20908kb
input:
5000 10000 53751618 18 124436 857 472 16 13506752 72 6929805 1313173 1 8 13 3 9428917 114702 15875684 375277 95772377 1 19 46 146 544774 2606 90182736 313 2 26253 330 92 17290550 229451029 53 3175 2 48316557 38441802 31 52027 40844521 966 2 455 40909310 6556 6662246 17592087 4914675 39 11812 4590536...
output:
24730200863
result:
ok single line: '24730200863'
Test #7:
score: 0
Accepted
time: 65ms
memory: 22576kb
input:
5000 10000 529152667 2028658 288974 46 2 1853274 212853 4442959 1 7439830 113977860 15476191 87430 6 972573 112375 4489 485 1273959 4049 4059 21 39694709 5 15511 256587916 2 164834468 4 95557537 9330 3 31231 1880144 197069 5753237 102274889 2187511 108044 1906 76869460 12 30311 27016904 6492296 1645...
output:
21376330041
result:
ok single line: '21376330041'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
3 2 1 1 999 1 1 999 1 999 999 1 999 999 999 1 999 999 1 999 5 1 999 1 1 3 100 1
output:
106
result:
ok single line: '106'