結果

問題 No.421 しろくろチョコレート
ユーザー Konton7Konton7
提出日時 2020-03-12 13:52:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 4,588 bytes
コンパイル時間 2,947 ms
コンパイル使用メモリ 212,328 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 15:00:07
合計ジャッジ時間 3,922 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define allpt(v) (v).begin(), (v).end()
#define allpt_r(v) (v).rbegin(), (v).rend()


const int mod = 1e9 + 7;
const string wsp = " ";
const string tb = "\t";
const string rt = "\n";

template <typename T>
void show1dvec(vector<T> v)
{
    if (v.size() == 0)
        return;
    int n = v.size() - 1;
    rep(i, n) cout << v[i] << wsp;
    cout << v[n] << rt;
    return;
}

template <typename T>
void show2dvec(vector<vector<T>> v)
{
    int n = v.size();
    rep(i, n) show1dvec(v[i]);
}

const int INTMAX = 2 * 1e9 + 10;
const int NMAX = 3 * 1e3;

bool check[NMAX];

template <typename T>
int dfs(int s, int t, T f, vector<pair<T, T>> edge[], vector<T> &used)
{
    int d = 0;
    check[s] = true;
    if (s == t)
    {
        used.push_back(t);
        return f;
    }

    for (auto next : edge[s])
    {
        if (!check[next.first] && next.second > 0)
            d = dfs(next.first, t, min(next.second, f), edge, used);
        if (d > 0)
        {
            used.push_back(s);
            return d;
        }
        // DFSが終点まで行くと正の値dが帰ってくるので、辺の容量の調整をして終了
    }
    return 0; //そうでなければ何もしない
}

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, m, h, w, s, t, c, a, b, f, adjust_pair, black, white, distant_pair, total_score;
    cin >> h >> w;
    n = h * w + 2;
    f = 1, adjust_pair = black = white = distant_pair = total_score = 0;
    vector<string> field(h);
    vector<pair<int, int>> edge[n];
    vector<int> used;
    rep(i, h)
    {
        cin >> field[i];
        rep(j, w)
        {
            if (field[i][j] == 'w')
            {
                white++;
                edge[0].push_back(make_pair(w * i + j + 1, 1));
                edge[w * i + j + 1].push_back(make_pair(0, 0));
            }
            else if (field[i][j] == 'b')
            {
                black++;
                edge[n - 1].push_back(make_pair(w * i + j + 1, 0));
                edge[w * i + j + 1].push_back(make_pair(n - 1, 1));
            }
        }
    }

    rep(i, h) rep(j, w - 1)
    {
        if (field[i][j] == 'w' && field[i][j + 1] == 'b')
        {
            edge[w * i + j + 1].push_back(make_pair(w * i + j + 2, 1));
            edge[w * i + j + 2].push_back(make_pair(w * i + j + 1, 0));
        }
        else if (field[i][j] == 'b' && field[i][j + 1] == 'w')
        {
            edge[w * i + j + 1].push_back(make_pair(w * i + j + 2, 0));
            edge[w * i + j + 2].push_back(make_pair(w * i + j + 1, 1));
        }
    }

    rep(i, h - 1) rep(j, w)
    {
        if (field[i][j] == 'w' && field[i + 1][j] == 'b')
        {
            edge[w * i + j + 1].push_back(make_pair(w * i + w + j + 1, 1));
            edge[w * i + w + j + 1].push_back(make_pair(w * i + j + 1, 0));
        }
        else if (field[i][j] == 'b' && field[i + 1][j] == 'w')
        {
            edge[w * i + j + 1].push_back(make_pair(w * i + w + j + 1, 0));
            edge[w * i + w + j + 1].push_back(make_pair(w * i + j + 1, 1));
        }
    }

    

    while (f > 0)
    {
        fill(check, check + n, false);

        f = dfs(0, n - 1, INTMAX, edge, used);
        a = -1, b = -1;
        for (auto i : used)
        {
            a = b;
            b = i;

            if (a >= 0 && b >= 0)
            {
                for (auto &j : edge[a])
                {
                    if (j.first == b)
                    {
                        j.second += f;
                        break;
                    }
                }
                for (auto &j : edge[b])
                {

                    if (j.first == a)
                    {
                        j.second -= f;
                        break;
                    }
                }
            }
        }
        adjust_pair += f;
        used.clear();
    }
    
    white -= adjust_pair, black -= adjust_pair;
    distant_pair = min(white, black);
    white -= distant_pair, black -= distant_pair;

    total_score = 100 * adjust_pair + 10 * distant_pair + white + black;
    cout << total_score << endl;
    return 0;
}
0