結果

問題 No.421 しろくろチョコレート
ユーザー xuzijian629xuzijian629
提出日時 2019-04-04 01:56:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 3,213 bytes
コンパイル時間 2,601 ms
コンパイル使用メモリ 210,064 KB
実行使用メモリ 15,980 KB
最終ジャッジ日時 2023-10-24 14:57:55
合計ジャッジ時間 4,621 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 14 ms
6,700 KB
testcase_02 AC 7 ms
4,348 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 6 ms
4,852 KB
testcase_08 AC 7 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 6 ms
5,084 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 22 ms
10,540 KB
testcase_17 AC 27 ms
10,444 KB
testcase_18 AC 31 ms
10,856 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 13 ms
7,268 KB
testcase_21 AC 16 ms
8,440 KB
testcase_22 AC 7 ms
5,188 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 12 ms
6,312 KB
testcase_29 AC 17 ms
7,396 KB
testcase_30 AC 15 ms
6,420 KB
testcase_31 AC 32 ms
15,980 KB
testcase_32 AC 14 ms
7,012 KB
testcase_33 AC 16 ms
7,024 KB
testcase_34 AC 7 ms
4,348 KB
testcase_35 AC 8 ms
4,348 KB
testcase_36 AC 9 ms
5,216 KB
testcase_37 AC 25 ms
9,740 KB
testcase_38 AC 29 ms
12,148 KB
testcase_39 AC 8 ms
4,928 KB
testcase_40 AC 8 ms
5,996 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 3 ms
4,348 KB
testcase_43 AC 8 ms
5,132 KB
testcase_44 AC 13 ms
8,072 KB
testcase_45 AC 4 ms
4,348 KB
testcase_46 AC 4 ms
4,348 KB
testcase_47 AC 12 ms
6,204 KB
testcase_48 AC 23 ms
11,260 KB
testcase_49 AC 8 ms
4,756 KB
testcase_50 AC 5 ms
4,348 KB
testcase_51 AC 18 ms
7,872 KB
testcase_52 AC 6 ms
4,900 KB
testcase_53 AC 4 ms
4,348 KB
testcase_54 AC 8 ms
4,400 KB
testcase_55 AC 3 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 3 ms
4,348 KB
testcase_58 AC 11 ms
6,532 KB
testcase_59 AC 4 ms
4,348 KB
testcase_60 AC 31 ms
12,972 KB
testcase_61 AC 30 ms
13,572 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
testcase_64 AC 7 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Hungarian {
    const int INF = 1e9;
    int n, p, q;
    vector<vector<int>> mat;
    vector<int> fx, fy, x, y;

    Hungarian(const vector<vector<int>>& mat) : n(mat.size()), mat(mat), fx(mat.size(), INF), fy(mat.size()), x(mat.size(), -1), y(mat.size(), -1) {}

    int 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;) {
            vector<int> 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) {
                int 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++;
            }
        }
        int 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() {
    int n, m;
    cin >> n >> m;
    vector<vector<int>> board(n, vector<int>(m, -1));
    int w = 0, b = 0;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < m; j++) {
            if (s[j] == 'w') board[i][j] = w++;
            else if (s[j] == 'b') board[i][j] = 10000 + b++;
        }
    }

    int k = max(w, b);
    vector<vector<int>> mat(k, vector<int>(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 <= board[i][j] && board[i][j] < 10000 && 10000 <= board[ii][jj]) {
                        if (is_next(i, j, ii, jj)) {
                            mat[board[i][j]][board[ii][jj] - 10000] = 100;
                        } else {
                            mat[board[i][j]][board[ii][jj] - 10000] = 10;
                        }
                    }
                }
            }
        }
    }

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