Submission #696129

# Submission time Handle Problem Language Result Execution time Memory
696129 2023-02-05T15:45:03 Z Nhoksocqt1 Domino (COCI15_domino) C++17
110 / 160
1083 ms 524288 KB
#include<bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ll> il;

template<class X, class Y>
	inline bool maximize(X &x, const Y &y) {return (x < y ? x = y, 1 : 0);}
template<class X, class Y>
	inline bool minimize(X &x, const Y &y) {return (x > y ? x = y, 1 : 0);}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int Random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

const int lx[] = {-1, 0, 0, 1}, ly[] = {0, -1, 1, 0};

class MaxFlowMinCost {
    private:
        vector<ll> dist;
        vector<int> head, point, next, flow, capa, cost, tr;
        int numNode, numEdge;

    public:
        MaxFlowMinCost(int _n = 0) {
            point.clear(), next.clear(), flow.clear();
            capa.clear(), cost.clear();
            numNode = _n, numEdge = 0;

            head.assign(_n + 7, -1);
            dist.assign(_n + 7, 0);
            tr.assign(_n + 7, 0);
        }

        void addEdge(int u, int v, int ca, int co) {
            point.push_back(v), flow.push_back(0), capa.push_back(ca);
            cost.push_back(co), next.push_back(head[u]), head[u] = numEdge++;

            point.push_back(u), flow.push_back(0), capa.push_back(0);
            cost.push_back(-co), next.push_back(head[v]), head[v] = numEdge++;
        }

        bool bellmanFord(int s, int t) {
            queue<int> qu;
            vector<bool> inQu(numNode + 7, 0);
            for (int i = 1; i <= numNode; ++i)
                dist[i] = 1e18, inQu[i] = 0;

            inQu[s] = 1, dist[s] = 0;
            qu.push(s);

            while(qu.size()) {
                int u(qu.front());
                qu.pop();

                inQu[u] = 0;
                for (int it = head[u]; it >= 0; it = next[it]) {
                    if(flow[it] < capa[it]) {
                        int v(point[it]);
                        if(dist[v] > dist[u] + cost[it]) {
                            dist[v] = dist[u] + cost[it];
                            tr[v] = it;

                            if(!inQu[v]) {
                                inQu[v] = 1;
                                qu.push(v);
                            }
                        }
                    }
                }
            }

            return (dist[t] < 1e18);
        }

        il maxFlow(int s, int t) {
            for (int i = 0; i < numEdge; ++i)
                flow[i] = 0;

            ll totCost(0);
            int totFlow(0);
            while(bellmanFord(s, t)) {
                int delta(1e9+7);
                for (int u = t; u != s; u = point[tr[u] ^ 1])
                    delta = min(delta, capa[tr[u]] - flow[tr[u]]);

                for (int u = t; u != s; u = point[tr[u] ^ 1]) {
                    flow[tr[u]] += delta;
                    flow[tr[u] ^ 1] -= delta;
                }

                totFlow += delta;
                totCost += dist[t] * delta;
            }

            return {totFlow, totCost};
        }
} G;

const int MAXN = 2003;

int val[MAXN][MAXN], nSize, k;

void process() {
    cin >> nSize >> k;

    G = MaxFlowMinCost(nSize * nSize + 3);
    G.addEdge(nSize * nSize + 3, nSize * nSize + 1, k, 0);

    ll sum(0);
    for (int i = 1; i <= nSize; ++i) {
        for (int j = 1; j <= nSize; ++j) {
            cin >> val[i][j];
            sum += val[i][j];
            if((i + j) & 1) {
                G.addEdge(nSize * nSize + 1, (i - 1) * nSize + j, 1, -val[i][j]);
                for (int id = 0; id < 4; ++id) {
                    int x(i + lx[id]), y(j + ly[id]);
                    if(x < 1 || y < 1 || x > nSize || y > nSize)
                        continue;

                    G.addEdge((i - 1) * nSize + j, (x - 1) * nSize + y, 1, 0);
                }
            } else {
                G.addEdge((i - 1) * nSize + j, nSize * nSize + 2, 1, -val[i][j]);
            }
        }
    }

    cout << sum + G.maxFlow(nSize * nSize + 3, nSize * nSize + 2).se;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

//    freopen("domino.inp", "r", stdin);
//    freopen("domino.out", "w", stdout);

    process();
    return 0;
}

Compilation message

domino.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("O3")
      | 
domino.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    7 | #pragma GCC optimization ("unroll-loops")
      |
# Verdict Execution time Memory Grader output
1 Correct 78 ms 37112 KB Output is correct
2 Correct 76 ms 37120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1057 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 921 ms 316392 KB Output is correct
2 Correct 863 ms 316424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 462 ms 143532 KB Output is correct
2 Correct 443 ms 143364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1032 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 852 KB Output is correct
2 Correct 2 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 998 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2244 KB Output is correct
2 Correct 5 ms 2244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1083 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1364 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 677 ms 143484 KB Output is correct
2 Correct 596 ms 143704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1033 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -