Submission #254070

# Submission time Handle Problem Language Result Execution time Memory
254070 2020-07-29T10:13:59 Z VEGAnn Domino (COCI15_domino) C++14
160 / 160
565 ms 209812 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 = 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
1 Correct 83 ms 105848 KB Output is correct
2 Correct 87 ms 103276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 95744 KB Output is correct
2 Correct 52 ms 95608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 549 ms 209468 KB Output is correct
2 Correct 503 ms 189396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 95352 KB Output is correct
2 Correct 59 ms 95352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 320 ms 165796 KB Output is correct
2 Correct 320 ms 172940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 130168 KB Output is correct
2 Correct 185 ms 122960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 565 ms 209656 KB Output is correct
2 Correct 526 ms 189532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 95736 KB Output is correct
2 Correct 60 ms 95608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 528 ms 209812 KB Output is correct
2 Correct 520 ms 189708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 96248 KB Output is correct
2 Correct 54 ms 96120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 95352 KB Output is correct
2 Correct 51 ms 95352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 525 ms 209488 KB Output is correct
2 Correct 507 ms 189656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 95992 KB Output is correct
2 Correct 54 ms 95864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 184 ms 130368 KB Output is correct
2 Correct 173 ms 123096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 95352 KB Output is correct
2 Correct 51 ms 95352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 537 ms 209252 KB Output is correct
2 Correct 532 ms 189652 KB Output is correct