結果

問題 No.421 しろくろチョコレート
ユーザー vorg101vorg101
提出日時 2017-05-13 14:29:04
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 4,279 bytes
コンパイル時間 1,990 ms
コンパイル使用メモリ 114,880 KB
実行使用メモリ 26,260 KB
最終ジャッジ日時 2024-09-23 07:15:41
合計ジャッジ時間 4,939 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
25,992 KB
testcase_01 AC 31 ms
25,940 KB
testcase_02 AC 31 ms
25,772 KB
testcase_03 AC 30 ms
23,948 KB
testcase_04 AC 31 ms
23,988 KB
testcase_05 AC 28 ms
23,836 KB
testcase_06 AC 29 ms
23,956 KB
testcase_07 AC 32 ms
26,020 KB
testcase_08 AC 29 ms
24,004 KB
testcase_09 AC 30 ms
24,088 KB
testcase_10 AC 30 ms
25,988 KB
testcase_11 AC 31 ms
24,092 KB
testcase_12 AC 29 ms
23,964 KB
testcase_13 AC 29 ms
25,996 KB
testcase_14 AC 29 ms
23,960 KB
testcase_15 AC 29 ms
24,076 KB
testcase_16 AC 34 ms
23,856 KB
testcase_17 AC 36 ms
25,944 KB
testcase_18 AC 35 ms
25,876 KB
testcase_19 AC 29 ms
23,964 KB
testcase_20 AC 33 ms
26,044 KB
testcase_21 AC 34 ms
23,828 KB
testcase_22 AC 32 ms
25,764 KB
testcase_23 AC 29 ms
23,852 KB
testcase_24 AC 28 ms
23,940 KB
testcase_25 AC 28 ms
23,832 KB
testcase_26 AC 28 ms
23,828 KB
testcase_27 AC 29 ms
23,948 KB
testcase_28 AC 30 ms
23,836 KB
testcase_29 AC 31 ms
26,000 KB
testcase_30 AC 33 ms
24,028 KB
testcase_31 AC 30 ms
22,192 KB
testcase_32 AC 32 ms
24,088 KB
testcase_33 AC 35 ms
23,832 KB
testcase_34 AC 29 ms
23,876 KB
testcase_35 AC 30 ms
23,744 KB
testcase_36 AC 32 ms
23,828 KB
testcase_37 AC 37 ms
25,944 KB
testcase_38 AC 38 ms
26,116 KB
testcase_39 AC 30 ms
21,808 KB
testcase_40 AC 32 ms
23,956 KB
testcase_41 AC 29 ms
23,956 KB
testcase_42 AC 29 ms
24,088 KB
testcase_43 AC 29 ms
23,964 KB
testcase_44 AC 33 ms
23,732 KB
testcase_45 AC 29 ms
23,964 KB
testcase_46 AC 30 ms
26,000 KB
testcase_47 AC 30 ms
23,960 KB
testcase_48 AC 32 ms
23,964 KB
testcase_49 AC 29 ms
23,980 KB
testcase_50 AC 28 ms
23,960 KB
testcase_51 AC 34 ms
26,036 KB
testcase_52 AC 31 ms
26,012 KB
testcase_53 AC 30 ms
24,108 KB
testcase_54 AC 28 ms
23,956 KB
testcase_55 AC 29 ms
25,860 KB
testcase_56 AC 29 ms
26,116 KB
testcase_57 AC 29 ms
23,832 KB
testcase_58 AC 31 ms
25,908 KB
testcase_59 AC 30 ms
25,988 KB
testcase_60 AC 39 ms
23,960 KB
testcase_61 AC 37 ms
24,072 KB
testcase_62 AC 28 ms
26,260 KB
testcase_63 AC 28 ms
24,080 KB
testcase_64 AC 28 ms
25,868 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;

class Program
{
    private static readonly Scanner sc = new Scanner();
    static void Main(string[] args)
    {
        int n = sc.Int;
        int m = sc.Int;

        string[] srkr = new string[n];

        Flow mf = new Flow(n * m + 2);
        int s = n * m;
        int t = n * m + 1;

        for (int i = 0; i < n; i++)
            srkr[i] = sc.Str;

        int cntw = 0;
        int cntb = 0;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (srkr[i][j] == 'w')
                {
                    mf.AddEdge(s, i * m + j, 1);
                    cntw++;
                }
                if (srkr[i][j] == 'b')
                {
                    mf.AddEdge(i * m + j, t, 1);
                    cntb++;
                }
            }
        }

        int[] dx = new int[] {1, -1, 0, 0};
        int[] dy = new int[] {0, 0, 1, -1};

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                for (int k = 0; k < 4; k++)
                {
                    int nx = i + dx[k], ny = j + dy[k];
                    if (nx < 0 || n <= nx || ny < 0 || m <= ny) continue;
                    if (srkr[nx][ny] == 'b')
                        mf.AddEdge(m * i + j, m * nx + ny, 1);
                }
            }
        }

        int cntpair = mf.MaximumFlow(s, t);
        int cnttwo = Math.Min(cntw, cntb) - cntpair;
        int cntone = Math.Max(cntw, cntb) - cntpair - cnttwo;

        Console.WriteLine(100 * cntpair + 10 * cnttwo + cntone);
    }
}

class Scanner
{
    private string[] _str = new string[0];
    private int _i;

    public string Str
    {
        get
        {
            if (_i >= _str.Length)
            {
                _str = Console.ReadLine().Split(' ');
                _i = 0;
            }
            return _str[_i++];
        }
    }

    public int Int { get { return int.Parse(Str); } }
    public long Long { get { return long.Parse(Str); } }
    public double Double { get { return double.Parse(Str); } }
}

class Flow
{
    private readonly List<Edge>[] _rn;
    private readonly int _n;
    private readonly int[] _level;
    private readonly int[] _itr;

    private class Edge
    {
        public int To;
        public int Cap;
        public int Rev;
    }

    public Flow(int n)
    {
        _n = n;
        _rn = new List<Edge>[n];
        _level = new int[n];
        _itr = new int[n];

        for (var i = 0; i < n; i++)
            _rn[i] = new List<Edge>();
    }

    public void AddEdge(int from, int to, int cap)
    {
        _rn[from].Add(new Edge { To = to, Cap = cap, Rev = _rn[to].Count });
        _rn[to].Add(new Edge { To = from, Cap = 0, Rev = _rn[from].Count-1 });
    }

    public int MaximumFlow(int s, int t)
    {
        var sum = 0;
        while (BFS(s, t))
        {
            int d;
            Array.Clear(_itr, 0, _n);
            while ((d = DFS(s, t, int.MaxValue)) != 0)
                sum += d;
        }
        return sum;
    }

    private bool BFS(int s, int t)
    {
        for (var i = 0; i < _n; i++)
            _level[i] = -1;

        var q = new Queue<int>();
        q.Enqueue(s);

        _level[s] = 0;

        while (q.Count != 0)
        {
            var v = q.Dequeue();
            foreach (var e in _rn[v])
            {
                if (e.Cap > 0 && _level[e.To] == -1)
                {
                    q.Enqueue(e.To);
                    _level[e.To] = _level[v] + 1;
                }
            }
        }

        return _level[t] != -1;
    }

    private int DFS(int v, int t, int f)
    {
        if (v == t)
            return f;

        for (; _itr[v] < _rn[v].Count; _itr[v]++)
        {
            var e = _rn[v][_itr[v]];
            var reve = _rn[e.To][e.Rev];
            if (e.Cap > 0 && _level[v] + 1 == _level[e.To])
            {
                var d = DFS(e.To, t, Math.Min(f, e.Cap));
                if (d != 0)
                {
                    e.Cap -= d;
                    reve.Cap += d;
                    return d;
                }
            }
        }

        return 0;
    }
}
0