This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//@EXPECED_GRADES@ AC TLE TLE AC TLE
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;
#include <bits/extc++.h>
const ll INF = numeric_limits<ll>::max() / 4;
typedef vector<ll> VL;
struct MCMF {
int N;
vector<vi> ed, red;
vector<VL> cap, flow, cost;
vi seen;
VL dist, pi;
vector<pii> par;
MCMF(int N) :
N(N), ed(N), red(N), cap(N, VL(N)), flow(cap), cost(cap),
seen(N), dist(N), pi(N), par(N) {}
void addEdge(int from, int to, ll cap, ll cost) {
this->cap[from][to] = cap;
this->cost[from][to] = cost;
ed[from].push_back(to);
red[to].push_back(from);
}
void path(int s) {
fill(all(seen), 0);
fill(all(dist), INF);
dist[s] = 0; ll di;
__gnu_pbds::priority_queue<pair<ll, int>> q;
vector<decltype(q)::point_iterator> its(N);
q.push({0, s});
auto relax = [&](int i, ll cap, ll cost, int dir) {
ll val = di - pi[i] + cost;
if (cap && val < dist[i]) {
dist[i] = val;
par[i] = {s, dir};
if (its[i] == q.end()) its[i] = q.push({-dist[i], i});
else q.modify(its[i], {-dist[i], i});
}
};
while (!q.empty()) {
s = q.top().second; q.pop();
seen[s] = 1; di = dist[s] + pi[s];
for (int i : ed[s]) if (!seen[i])
relax(i, cap[s][i] - flow[s][i], cost[s][i], 1);
for (int i : red[s]) if (!seen[i])
relax(i, flow[i][s], -cost[i][s], 0);
}
rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
}
pair<ll, ll> maxflow(int s, int t) {
ll totflow = 0, totcost = 0;
while (path(s), seen[t]) {
ll fl = INF;
for (int p,r,x = t; tie(p,r) = par[x], x != s; x = p)
fl = min(fl, r ? cap[p][x] - flow[p][x] : flow[x][p]);
totflow += fl;
for (int p,r,x = t; tie(p,r) = par[x], x != s; x = p)
if (r) flow[p][x] += fl;
else flow[x][p] -= fl;
}
rep(i,0,N) rep(j,0,N) totcost += cost[i][j] * flow[i][j];
return {totflow, totcost};
}
// If some costs can be negative, call this before maxflow:
void setpi(int s) { // (otherwise, leave this out)
fill(all(pi), INF); pi[s] = 0;
int it = N, ch = 1; ll v;
while (ch-- && it--)
rep(i,0,N) if (pi[i] != INF)
for (int to : ed[i]) if (cap[i][to])
if ((v = pi[i] + cost[i][to]) < pi[to])
pi[to] = v, ch = 1;
assert(it >= 0); // negative cost cycle
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n;
cin>>n;
vi x(n), y(n);
rep(i,0,n) cin>>x[i];
rep(i,0,n) cin>>y[i];
int source = 2*n, sink = 2*n+1;
MCMF mcmf(2*n+2);
rep(i,0,n) {
mcmf.addEdge(source, i, y[i], 0);
mcmf.addEdge(n+i, sink, x[i], 0);
rep(j,0,n) {
mcmf.addEdge(i, n+j, 1e9, j < i ? -1 : j == i ? 0 : 1);
}
}
mcmf.setpi(source);
auto [flow, cost] = mcmf.maxflow(source, sink);
assert(flow == accumulate(all(y),0));
cout << -cost << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |