This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define PB push_back
#define i2 array<int,2>
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int N = 2010;
const int oo = 2e9;
const int cnst = 100;
vector<i2> who[N];
ll glob = 0;
int n, k, a[N][N], S, T, GO[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int dst[N * N], pre[N * N];
bool good[N][N];
struct edge{
int a, b, cap, flow, cost;
edge(): a(0), b(0), cap(0), flow(0), cost(0) {}
edge(int _a, int _b, int c, int f, int co): a(_a), b(_b), cap(c), flow(f), cost(co) {}
};
vector<edge> edges;
vector<int> g[N * N];
void add_edge(int a, int b, int cst){
g[a].PB(sz(edges));
edges.PB(edge(a, b, 1, 0, cst));
g[b].PB(sz(edges));
edges.PB(edge(b, a, 0, 0, -cst));
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef _LOCAL
freopen("in.txt","r",stdin);
#endif // _LOCAL
cin >> n >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
cin >> a[i][j];
glob += a[i][j];
}
S = n * n;
T = n * n + 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++){
if (i > 0)
who[a[i][j] + a[i - 1][j]].PB({i * n + j, (i - 1) * n + j});
if (j > 0)
who[a[i][j] + a[i][j - 1]].PB({i * n + j, i * n + j - 1});
}
int rem = cnst;
for (int i = N - 1; i >= 0 && rem > 0; i--)
for (int itr = 0; itr < sz(who[i]) && rem > 0; itr++){
i2 cur = who[i][itr];
good[cur[0] / n][cur[0] % n] = 1;
good[cur[1] / n][cur[1] % n] = 1;
rem--;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if ((i + j) & 1){
if (good[i][j])
add_edge(i * n + j, T, 0);
} else {
if (!good[i][j]) continue;
add_edge(S, i * n + j, 0);
// add edges to the neighbours
for (int it = 0; it < 4; it++){
int x = i + GO[it][0];
int y = j + GO[it][1];
// if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (x < 0 || x >= n || y < 0 || y >= n || !good[x][y]) continue;
add_edge(i * n + j, x * n + y, -(a[i][j] + a[x][y]));
}
}
ll cost = 0;
for (int itr = 0; itr < k; itr++){
fill(dst, dst + T + 1, oo);
dst[S] = 0;
for (int nit = 0; nit < 2 * cnst; nit++){
for (int idx = 0; idx < sz(edges); idx++) {
edge cr = edges[idx];
if (dst[cr.a] < oo && cr.flow < cr.cap && dst[cr.b] > dst[cr.a] + cr.cost) {
dst[cr.b] = dst[cr.a] + cr.cost;
pre[cr.b] = idx;
}
}
}
assert(dst[T] < oo);
cost += dst[T];
int pos = T;
while (pos != S){
int id = pre[pos];
edges[id].flow++;
edges[id ^ 1].flow--;
pos = edges[id].a;
}
}
cout << glob + cost;
return 0;
}
# | 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... |
# | 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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |