답안 #255359

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
255359 2020-07-31T19:33:43 Z ne4eHbKa Domino (COCI15_domino) C++17
160 / 160
2184 ms 269980 KB
//{ <defines>
#include <bits/stdc++.h>
using namespace std;

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,-O3")

#define fr(i, n) for(int i = 0; i < n; ++i)
#define fo(n) fr(i, n)

#define re return
#define ef else if
#define ifn(x) if(!(x))
#define _  << ' ' <<

#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back

#define sz(x) int((x).size())
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset((x), (y), sizeof (x))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ve<int> vi;

inline ll time() {re chrono :: system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
mt19937_64 RND(time());

template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}

int md = 998244353;

inline int m_add(int&a, int b) {a += b; if(a < 0) a += md; if(a >= md) a -= md; re a;}
inline int m_sum(int a, int b) {a += b; if(a < 0) a += md; if(a >= md) a -= md; re a;}
inline int m_mul(int&a, int b) {re a = 1ll * a * b % md;}
inline int m_prod(int a, int b) {re 1ll * a * b % md;}

int m_bpow(ll A, ll b) {
    int a = A % md;
    ll ans = 1;
    for(ll p = 1ll << 63 - __builtin_clzll(b); p; p >>= 1) {
        (ans *= ans) %= md;
        if(p & b)
            (ans *= a) %= md;
    }
    re ans;
}

//const ld pi = arg(complex<ld>(-1, 0));
//const ld pi2 = pi + pi;
const int oo = 2e9;
const ll OO = 4e18;
//} </defines>
const int N = 2e3 + 5;

int k, n;

vi d, p;

struct e {
    int u, v, capacity, flow, cost;
    bool full() { return flow == capacity; }
};

vector<e> E;

void add_edge(int u, int v, int c) {
    E.push_back({u, v, 1, 0, +c});
    E.push_back({v, u, 0, 0, -c});
}

map<pii, int> nm;

short mp[N][N];

vector<bool> mk;

vector< list<e*> > g;

void fb() {
    p.assign(n, +oo);
    p[0] = 0;
    fo(n) for(e &i : E) ifn(i.full())
        umin(p[i.v], p[i.u] + i.cost);
}

void dk() {
    d.assign(n, +oo);
    d[0] = 0;
    struct cmp {
        bool operator() (const int &a, const int &b) const
            { return d[a] != d[b] ? d[a] < d[b] : a < b; }
    };
    set<int, cmp> q;
    q.insert(0);
    while(!q.empty()) {
        int u = *q.begin();
        q.erase(q.begin());
        for(e *i : g[u]) {
            if(i->full()) continue;
            int v = i->v;
            int w = d[u] + i->cost + p[u] - p[v];
            if(d[v] <= w) continue;
            if(q.count(v)) q.erase(v);
            d[v] = w;
            q.insert(v);
        }
    }
    fo(n) if(d[i] < oo) p[i] += d[i];
    for(e &i : E) ifn(i.full()) assert(p[i.u] + i.cost - p[i.v] >= 0);
}

int ff(int v = 0) {
    if(mk[v]) re 1;
    mk[v] = true;
    if(v == 1) re 0;
    for(e *i : g[v]) {
        if(i->full()) continue;
        int u = i->v;
        if(i->cost + p[v] != p[u]) continue;
        int f = ff(u);
        if(f == 1) continue;
        i->flow++;
        E[1 ^ i - &E[0]].flow--;
        return f + i->cost;
    }
    return 1;
}

int field(const pii &p) { return mp[p.ft][p.sd]; }

void solve() {
    int t;
    cin >> t >> k;
    ll sum = 0;
    E.clear();
    fo(t) fr(j, t) {
        cin >> mp[i][j];
        sum += mp[i][j];
    }
    struct dom {
        int x, y, X, Y, v;
        dom(int fx, int fy, bool f): x(fx), y(fy), X(fx), Y(fy) {
            (f ? X : Y)++;
            if(x + y & 1) swap(x, X), swap(y, Y);
            v = mp[x][y] + mp[X][Y];
        }
        void mark() {
            nm[{x, y}];
            nm[{X, Y}];
        }
        int fi() { re nm[{x, y}]; }
        int se() { re nm[{X, Y}]; }
        bool operator< (const dom &f) const
            { return v > f.v; }
    };
    vector<dom> D;
    fo(t) fr(j, t) {
        if(i + 1 < t) D.eb(i, j, true);
        if(j + 1 < t) D.eb(i, j, false);
    }
    vector<dom*> pD;
    for(auto &i : D) pD.pb(&i);
    sort(bnd(pD), [&] (const dom* a, const dom *b) {return *a < *b;});
    int z = min(sz(D), (k - 1) * 7 + 1);
    nm.clear();
    fo(z) pD[i]->mark();
    n = 2;
    for(auto &i : nm) {
        i.sd = n++;
        if(i.ft.ft + i.ft.sd & 1) {
            add_edge(i.sd, 1, -field(i.ft));
        } else {
            add_edge(0, i.sd, -field(i.ft));
        }
    }
    fo(z) add_edge(pD[i]->fi(), pD[i]->se(), 0);
    g.clear();
    g.resize(n);
    for(auto &i : E) g[i.u].pb(&i);
    int ans = 0;
    fb();
    fo(k) {
        dk();
        mk.assign(n, false);
        ans += ff();
    }
    cout << sum + ans << endl;
}

int main() {
#ifdef _LOCAL
    freopen("in.txt", "r", stdin);
    int tests; cin >> tests;
    for(int test = 1; test <= tests; ++test) {
		cerr << "case #" << test << endl;
        solve();
        cerr << endl;
    }
#else
//    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
    solve();
#endif
    return 0;
}

Compilation message

domino.cpp: In function 'int m_bpow(ll, ll)':
domino.cpp:50:26: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
     for(ll p = 1ll << 63 - __builtin_clzll(b); p; p >>= 1) {
                       ~~~^~~~~~~~~~~~~~~~~~~~
domino.cpp: In function 'int ff(int)':
domino.cpp:133:17: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
         E[1 ^ i - &E[0]].flow--;
domino.cpp: In constructor 'solve()::dom::dom(int, int, bool)':
domino.cpp:154:18: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
             if(x + y & 1) swap(x, X), swap(y, Y);
                ~~^~~
domino.cpp: In function 'void solve()':
domino.cpp:180:20: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
         if(i.ft.ft + i.ft.sd & 1) {
                    ^
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 21088 KB Output is correct
2 Correct 45 ms 21088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 768 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2141 ms 267480 KB Output is correct
2 Correct 762 ms 268812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1071 ms 201356 KB Output is correct
2 Correct 477 ms 199612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 427 ms 79368 KB Output is correct
2 Correct 183 ms 79368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2108 ms 268608 KB Output is correct
2 Correct 732 ms 269980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 800 KB Output is correct
2 Correct 1 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2184 ms 268584 KB Output is correct
2 Correct 806 ms 268232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1596 KB Output is correct
2 Correct 3 ms 1688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2166 ms 268072 KB Output is correct
2 Correct 815 ms 268308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1120 KB Output is correct
2 Correct 2 ms 1120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 488 ms 79364 KB Output is correct
2 Correct 190 ms 79368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2127 ms 267796 KB Output is correct
2 Correct 766 ms 267792 KB Output is correct