結果

問題 No.421 しろくろチョコレート
ユーザー maimai
提出日時 2017-10-09 23:57:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 9,479 bytes
コンパイル時間 4,017 ms
コンパイル使用メモリ 236,904 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-23 07:16:00
合計ジャッジ時間 5,339 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 5 ms
6,940 KB
testcase_17 AC 4 ms
6,940 KB
testcase_18 AC 5 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 4 ms
6,944 KB
testcase_21 AC 4 ms
6,944 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 3 ms
6,940 KB
testcase_30 AC 3 ms
6,940 KB
testcase_31 AC 3 ms
6,944 KB
testcase_32 AC 3 ms
6,944 KB
testcase_33 AC 4 ms
6,940 KB
testcase_34 AC 3 ms
6,940 KB
testcase_35 AC 3 ms
6,940 KB
testcase_36 AC 3 ms
6,944 KB
testcase_37 AC 4 ms
6,940 KB
testcase_38 AC 5 ms
6,940 KB
testcase_39 AC 3 ms
6,944 KB
testcase_40 AC 3 ms
6,944 KB
testcase_41 AC 3 ms
6,944 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 3 ms
6,944 KB
testcase_44 AC 3 ms
6,940 KB
testcase_45 AC 3 ms
6,940 KB
testcase_46 AC 2 ms
6,944 KB
testcase_47 AC 4 ms
6,944 KB
testcase_48 AC 3 ms
6,940 KB
testcase_49 AC 3 ms
6,944 KB
testcase_50 AC 2 ms
6,940 KB
testcase_51 AC 3 ms
6,944 KB
testcase_52 AC 3 ms
6,940 KB
testcase_53 AC 2 ms
6,944 KB
testcase_54 AC 3 ms
6,944 KB
testcase_55 AC 2 ms
6,940 KB
testcase_56 AC 2 ms
6,940 KB
testcase_57 AC 2 ms
6,940 KB
testcase_58 AC 4 ms
6,944 KB
testcase_59 AC 2 ms
6,944 KB
testcase_60 AC 5 ms
6,940 KB
testcase_61 AC 5 ms
6,940 KB
testcase_62 AC 2 ms
6,940 KB
testcase_63 AC 2 ms
6,940 KB
testcase_64 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ライブラリ整備用提出

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include "bits/stdc++.h" // define macro "/D__MAI"

using namespace std;
typedef long long int ll;

#define xprintf(fmt,...) fprintf(stderr,fmt,__VA_ARGS__)
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;}
#define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout<<endl;}}
#define ALL(v) (v).begin(),(v).end()
#define repeat(cnt,l) for(auto cnt=0ll;(cnt)<(l);++(cnt))
#define iterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);++(cnt))
#define MD 1000000007ll
#define PI 3.1415926535897932384626433832795
#define EPS 1e-12
template<typename T1, typename T2> ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << "(" << p.first << ":" << p.second << ")"; return o; }
template<typename iterator> inline size_t argmin(iterator begin, iterator end) { return distance(begin, min_element(begin, end)); }
template<typename iterator> inline size_t argmax(iterator begin, iterator end) { return distance(begin, max_element(begin, end)); }
template<typename T> T& maxset(T& to, const T& val) { return to = max(to, val); }
template<typename T> T& minset(T& to, const T& val) { return to = min(to, val); }

mt19937_64 randdev(8901016);
inline ll rand_range(ll l, ll h) {
    return uniform_int_distribution<ll>(l, h)(randdev);
}

#ifdef __MAI
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
#ifdef __VSCC
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#endif
namespace {
#define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
    class MaiScanner {
    public:
        template<typename T> void input_integer(T& var) {
            var = 0;
            T sign = 1;
            int cc = getchar_unlocked();
            for (; cc<'0' || '9'<cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc&&cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var*sign;
        }
        inline int c() { return getchar_unlocked(); }
        inline MaiScanner& operator>>(int& var) {
            input_integer<int>(var);
            return *this;
        }
        inline MaiScanner& operator>>(long long& var) {
            input_integer<long long>(var);
            return *this;
        }
        inline MaiScanner& operator>>(string& var) {
            int cc = getchar_unlocked();
            for (; !isvisiblechar(cc); cc = getchar_unlocked());
            for (; isvisiblechar(cc); cc = getchar_unlocked())
                var.push_back(cc);
            return *this;
        }
        template<typename IT> void in(IT begin, IT end) {
            for (auto it = begin; it != end; ++it) *this >> *it;
        }
    };
}
MaiScanner scanner;




class Flow {
public:
    typedef int cap_t;
    size_t n;
    struct Arrow {
        int from, to;
        int left;
        cap_t cap;

        Arrow(int from = 0, int to = 0, cap_t w = 1) :from(from), to(to), left(w), cap(w) {}
        bool operator<(const Arrow& a) const { return (left != a.left) ? left < a.left : (left<a.left) | (cap<a.cap) | (from<a.from) | (to<a.to); }
        bool operator==(const Arrow& a) const { return (from == a.from) && (to == a.to) && (left == a.left) && (cap == a.cap); }
    };
    vector<vector<int>> vertex_to;
    vector<vector<int>> vertex_from;
    vector<Arrow> arrow;

    Flow(int n, int m = 5010) :n(n), vertex_to(n), vertex_from(n) { arrow.reserve(m); }

    void connect(int from, int to, cap_t left) {
        vertex_to[from].push_back(arrow.size()); // toto
        vertex_from[to].push_back(arrow.size()); // fromfrom
        arrow.emplace_back(from, to, left);
    }
    size_t degree(int v) {
        return vertex_to[v].size() + vertex_from[v].size();
    }
    size_t degree_in(int v) {
        return vertex_from[v].size();
    }
    size_t degree_out(int v) {
        return vertex_to[v].size();
    }
};


// -------------------------------------------------------------------
// dinic maxflow
// -------------------------------------------------------------------
// http://tubo28.me/algorithm/dinic/
// http://topcoder.g.hatena.ne.jp/Mi_Sawa/20140311

// DAG
int _dinic_path_dfs(Flow& flow, vector<int>& result, vector<int>& flag, const vector<int>& dist, int u, int i_sink, int mini) {
    // TODO: 経路再利用
    if (i_sink == u) return mini;
    if (flag[u]) return -1;
    flag[u] = true;

    int sumw = 0;
    bool term = true;
    for (int e : flow.vertex_to[u]) {
        Flow::Arrow& a = flow.arrow[e];
        if (a.left > 0 && dist[u]>dist[a.to]) {
            int w;
            if (mini < 0)
                w = a.left;
            else
                w = min(a.left, mini);

            w = _dinic_path_dfs(flow, result, flag, dist, a.to, i_sink, w);
            if (w == -1) continue;
            a.left -= w;
            result[a.to] += w;

            sumw += w;
            mini -= w;
            term = false;
            flag[u] = false; // TODO: 末尾では? 

            if (mini == 0) return term ? -1 : sumw;
        }
    }
    for (int e : flow.vertex_from[u]) {
        Flow::Arrow& a = flow.arrow[e];
        if (a.cap>a.left && dist[u]>dist[a.from]) {
            int w;
            if (mini < 0)
                w = a.cap - a.left;
            else
                w = min(a.cap - a.left, mini);

            w = _dinic_path_dfs(flow, result, flag, dist, a.from, i_sink, w);
            if (w == -1) continue;
            a.left += w;
            result[a.to] -= w;

            sumw += w;
            mini -= w;
            term = false;
            flag[u] = false;
            if (mini == 0) return term ? -1 : sumw;
        }
    }
    return term ? -1 : sumw;
}

// flowは書き換えられる.
void dinic(Flow &flow, vector<int>& result, int i_source, int i_sink) {
    assert(i_source != i_sink);

    result.resize(flow.n);

    int distbegin = 0;
    vector<int> dist(flow.n);
    queue<int> q;
    vector<int> flag(flow.n);
    for (int distbegin = 0; ; distbegin += flow.n) {

        q.emplace(i_sink); // bfsはsinkからsourceへの距離を計算.
        dist[i_sink] = distbegin + 1;
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int ie : flow.vertex_from[v]) {
                const Flow::Arrow& e = flow.arrow[ie];
                if (0<e.left && dist[e.from] <= distbegin) {
                    dist[e.from] = dist[v] + 1;
                    q.emplace(e.from);
                }
            }
            for (int ie : flow.vertex_to[v]) {
                const Flow::Arrow& e = flow.arrow[ie];
                if (e.left<e.cap && dist[e.to] <= distbegin) {
                    dist[e.to] = dist[v] + 1;
                    q.emplace(e.to);
                }
            }
        }
        //debugv(dist);
        fill(ALL(flag), false);

        if (dist[i_source] <= distbegin) {
            break;
        }
        else {
            result[i_source] += _dinic_path_dfs(flow, result, flag, dist, i_source, i_sink, -1);
        }
    }

}





class BipartiteMatching {
public:
    Flow flow;
    int n, m;
    int ss;

    BipartiteMatching(int n, int m) :flow(2 + n + m), n(n), m(m), ss(n + m) {
        for (int i = 0; i<n; i++)
            flow.connect(ss, i, 1);
        for (int i = 0; i<m; i++)
            flow.connect(n + i, ss + 1, 1);
    }

    void connect(int l, int r) {
        flow.connect(l, r + n, 1);
    }

    int solve_dinic(set<Flow::Arrow>& result) {
        int count = 0;
        vector<int> r;
        dinic(flow, r, ss, ss + 1);

        for (Flow::Arrow& a : flow.arrow) {
            if (a.from == ss || a.to == ss + 1) continue;
            if (a.left == 0) {
                result.insert(a);
                count++;
            }
        }
        return count;
    }
};


// =============================================================
// https://yukicoder.me/submissions/144370
// =============================================================


int width, height;
int m, n;

string cho[55];

int main() {
    int i, j, k;
    int x, y, a, b;

    cin >> height >> width;

    n = height*width;

    BipartiteMatching bm(n, n);

    for (y = 0; y<height; y++) {
        cin >> cho[y];
    }
    int white, black;
    white = black = 0;
    for (y = 0; y<height; y++) {
        for (x = 0; x<width; x++) {
            if (cho[y][x] == '.') continue;
            if (cho[y][x] == 'w')
                white++;
            else
                black++;
            if ((y^x) & 1) continue;
            if (0 < x && cho[y][x - 1] != '.')
                bm.connect(x + y*width, x - 1 + y*width);
            if (0 < y && cho[y - 1][x] != '.')
                bm.connect(x + y*width, x + (y - 1)*width);
            if (width - 1 > x && cho[y][x + 1] != '.')
                bm.connect(x + y*width, x + 1 + y*width);
            if (height - 1 > y && cho[y + 1][x] != '.')
                bm.connect(x + y*width, x + (y + 1)*width);

        }
    }

    set<Flow::Arrow> pairs;

    bm.solve_dinic(pairs);

    int np = pairs.size();
    white -= np;
    black -= np;

    cout << ((np * 100) + (min(white, black) * 10) + abs(white - black)) << endl;


    return 0;
}
0