結果

問題 No.421 しろくろチョコレート
ユーザー xuzijian629xuzijian629
提出日時 2018-11-07 01:39:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 3,444 bytes
コンパイル時間 2,189 ms
コンパイル使用メモリ 210,880 KB
実行使用メモリ 27,740 KB
最終ジャッジ日時 2023-10-24 14:56:13
合計ジャッジ時間 4,326 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 16 ms
9,260 KB
testcase_02 AC 7 ms
4,508 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 7 ms
5,564 KB
testcase_08 AC 6 ms
4,508 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 7 ms
6,092 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 25 ms
16,916 KB
testcase_17 AC 30 ms
16,652 KB
testcase_18 AC 33 ms
17,768 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 15 ms
10,316 KB
testcase_21 AC 18 ms
12,692 KB
testcase_22 AC 7 ms
6,420 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 13 ms
8,656 KB
testcase_29 AC 18 ms
10,580 KB
testcase_30 AC 15 ms
8,732 KB
testcase_31 AC 40 ms
27,740 KB
testcase_32 AC 16 ms
9,788 KB
testcase_33 AC 18 ms
9,788 KB
testcase_34 AC 6 ms
4,348 KB
testcase_35 AC 7 ms
4,348 KB
testcase_36 AC 8 ms
6,356 KB
testcase_37 AC 28 ms
15,332 KB
testcase_38 AC 33 ms
20,084 KB
testcase_39 AC 8 ms
5,828 KB
testcase_40 AC 9 ms
7,940 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 3 ms
4,348 KB
testcase_43 AC 9 ms
6,092 KB
testcase_44 AC 15 ms
11,900 KB
testcase_45 AC 4 ms
4,348 KB
testcase_46 AC 4 ms
4,348 KB
testcase_47 AC 13 ms
8,204 KB
testcase_48 AC 26 ms
18,236 KB
testcase_49 AC 8 ms
5,300 KB
testcase_50 AC 5 ms
4,348 KB
testcase_51 AC 19 ms
11,636 KB
testcase_52 AC 7 ms
5,564 KB
testcase_53 AC 4 ms
4,348 KB
testcase_54 AC 7 ms
4,772 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 3 ms
4,348 KB
testcase_57 AC 3 ms
4,348 KB
testcase_58 AC 13 ms
8,996 KB
testcase_59 AC 4 ms
4,348 KB
testcase_60 AC 36 ms
21,668 KB
testcase_61 AC 35 ms
23,116 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 6 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

// 二部グラフの最大重みマッチング
class Hungarian {
    int n, p, q;
    vvi mat;
    vi fx, fy, x, y;
    const i64 INF = 1e9;

public:
    Hungarian(const vvi& mat) : mat(mat) {
        n = mat.size();
        fx.assign(n, INF);
        fy.assign(n, 0);
        x.assign(n, -1);
        y.assign(n, -1);
    } 

    i64 run() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                fx[i] = max(fx[i], mat[i][j]);
            }
        }

        for (int i = 0; i < n;) {
            vi t(n, -1), s(n + 1, i);
            for (p = q = 0; p <= q && x[i] < 0; p++) {
                for (int k = s[p], j = 0; j < n && x[i] < 0; j++) {
                    if (fx[k] + fy[j] == mat[k][j] && t[j] < 0) {
                        s[++q] = y[j];
                        t[j] = k;
                        if (s[q] < 0) {
                            for (p = j; p >= 0; j = p) {
                                y[j] = k = t[j];
                                p = x[k];
                                x[k] = j;
                            }
                        }
                    }
                }
            }
            if (x[i] < 0) {
                i64 d = INF;
                for (int k = 0; k <= q; k++) {
                    for (int j = 0; j < n; j++) {
                        if (t[j] < 0) {
                            d = min(d, fx[s[k]] + fy[j] - mat[s[k]][j]);
                        }
                    }
                }
                for (int j = 0; j < n; j++) {
                    fy[j] += (t[j] < 0 ? 0 : d);
                }
                for (int k = 0; k <= q; k++) {
                    fx[s[k]] -= d;
                }
            } else {
                i++;
            }
        }
        i64 ret = 0;
        for (int i = 0; i < n; i++) {
            ret += mat[i][x[i]];
        }
        return ret;
    }

    int match_y(int k) {
        return x[k];
    }

    int match_x(int k) {
        return y[k];
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout.setf(ios::fixed);
    cout.precision(10);
    int n, m;
    cin >> n >> m;
    vvi b(n, vi(m, -1));
    int wcnt = 0, bcnt = 0;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < m; j++) {
            if (s[j] == 'w') {
                b[i][j] = wcnt++;
            } else if (s[j] == 'b') {
                b[i][j] = 10000 + bcnt++;
            }
        }
    }

    int k = max(wcnt, bcnt);
    vvi mat(k, vi(k));

    auto is_next = [](int i, int j, int ii, int jj) {
        return abs(i - ii) + abs(j - jj) == 1;
    };
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            for (int ii = 0; ii < n; ii++) {
                for (int jj = 0; jj < m; jj++) {
                    if (0 <= b[i][j] && b[i][j] < 10000 && 10000 <= b[ii][jj]) {
                        if (is_next(i, j, ii, jj)) {
                            mat[b[i][j]][b[ii][jj] - 10000] = 100;
                        } else {
                            mat[b[i][j]][b[ii][jj] - 10000] = 10;
                        }
                    }
                }
            }
        }
    }

    Hungarian h(mat);
    cout << h.run() + abs(wcnt - bcnt) << endl;
}
0