Submission #255368

# Submission time Handle Problem Language Result Execution time Memory
255368 2020-07-31T20:03:47 Z VEGAnn Domino (COCI15_domino) C++14
160 / 160
545 ms 211448 KB
#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 = 50;
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);

            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 || !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;
                }
            }
        }

        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
1 Correct 92 ms 105720 KB Output is correct
2 Correct 90 ms 103528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 95736 KB Output is correct
2 Correct 54 ms 95752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 516 ms 209656 KB Output is correct
2 Correct 513 ms 191340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 95356 KB Output is correct
2 Correct 63 ms 95352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 338 ms 166552 KB Output is correct
2 Correct 314 ms 174200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 131064 KB Output is correct
2 Correct 171 ms 124224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 540 ms 211140 KB Output is correct
2 Correct 522 ms 191900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 95736 KB Output is correct
2 Correct 61 ms 95644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 541 ms 211448 KB Output is correct
2 Correct 515 ms 191964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 96248 KB Output is correct
2 Correct 68 ms 96120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 95352 KB Output is correct
2 Correct 55 ms 95352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 541 ms 211296 KB Output is correct
2 Correct 515 ms 192308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 95864 KB Output is correct
2 Correct 58 ms 95864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 131576 KB Output is correct
2 Correct 178 ms 124756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 95352 KB Output is correct
2 Correct 55 ms 95352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 545 ms 211448 KB Output is correct
2 Correct 518 ms 191968 KB Output is correct