結果

問題 No.421 しろくろチョコレート
ユーザー eitahoeitaho
提出日時 2016-09-09 23:06:09
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 5,789 bytes
コンパイル時間 1,089 ms
コンパイル使用メモリ 116,012 KB
実行使用メモリ 27,520 KB
最終ジャッジ日時 2024-09-23 07:06:05
合計ジャッジ時間 4,721 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
27,136 KB
testcase_01 AC 33 ms
25,388 KB
testcase_02 AC 32 ms
25,224 KB
testcase_03 AC 30 ms
25,336 KB
testcase_04 AC 31 ms
25,248 KB
testcase_05 AC 31 ms
25,360 KB
testcase_06 AC 31 ms
27,512 KB
testcase_07 AC 32 ms
25,476 KB
testcase_08 AC 32 ms
25,608 KB
testcase_09 AC 31 ms
27,388 KB
testcase_10 AC 31 ms
27,260 KB
testcase_11 AC 32 ms
25,096 KB
testcase_12 AC 30 ms
25,476 KB
testcase_13 AC 30 ms
25,080 KB
testcase_14 AC 31 ms
25,352 KB
testcase_15 AC 30 ms
25,136 KB
testcase_16 AC 34 ms
25,328 KB
testcase_17 AC 35 ms
25,460 KB
testcase_18 AC 35 ms
25,080 KB
testcase_19 AC 30 ms
27,248 KB
testcase_20 AC 33 ms
25,276 KB
testcase_21 AC 35 ms
25,196 KB
testcase_22 AC 32 ms
25,104 KB
testcase_23 AC 30 ms
25,476 KB
testcase_24 AC 30 ms
25,092 KB
testcase_25 AC 30 ms
25,348 KB
testcase_26 AC 30 ms
25,216 KB
testcase_27 AC 30 ms
25,136 KB
testcase_28 AC 32 ms
25,456 KB
testcase_29 AC 31 ms
25,216 KB
testcase_30 AC 33 ms
27,368 KB
testcase_31 AC 42 ms
27,520 KB
testcase_32 AC 33 ms
25,200 KB
testcase_33 AC 33 ms
23,032 KB
testcase_34 AC 31 ms
25,136 KB
testcase_35 AC 31 ms
25,388 KB
testcase_36 AC 32 ms
25,088 KB
testcase_37 AC 35 ms
25,320 KB
testcase_38 AC 39 ms
25,468 KB
testcase_39 AC 31 ms
25,252 KB
testcase_40 AC 33 ms
25,216 KB
testcase_41 AC 31 ms
25,136 KB
testcase_42 AC 32 ms
27,272 KB
testcase_43 AC 32 ms
25,136 KB
testcase_44 AC 35 ms
25,264 KB
testcase_45 AC 31 ms
25,348 KB
testcase_46 AC 31 ms
25,228 KB
testcase_47 AC 33 ms
25,136 KB
testcase_48 AC 39 ms
25,276 KB
testcase_49 AC 31 ms
25,348 KB
testcase_50 AC 31 ms
25,344 KB
testcase_51 AC 34 ms
25,220 KB
testcase_52 AC 32 ms
25,088 KB
testcase_53 AC 30 ms
25,476 KB
testcase_54 AC 31 ms
25,264 KB
testcase_55 AC 31 ms
25,348 KB
testcase_56 AC 31 ms
25,264 KB
testcase_57 AC 31 ms
25,216 KB
testcase_58 AC 33 ms
27,444 KB
testcase_59 AC 30 ms
25,216 KB
testcase_60 AC 41 ms
23,352 KB
testcase_61 AC 38 ms
27,376 KB
testcase_62 AC 30 ms
25,008 KB
testcase_63 AC 31 ms
25,348 KB
testcase_64 AC 30 ms
25,392 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.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using Enu = System.Linq.Enumerable;

public class Program
{
    static readonly char White = 'w', Black = 'b';
    static readonly int[] DR = { 0, 1, 0, -1 };
    static readonly int[] DC = { 1, 0, -1, 0 };

    public void Solve()
    {
        int H = Reader.Int(), W = Reader.Int();
        var grid = Reader.StringArray(H);
        int S = H * W, T = S + 1;
        var dinic = new Dinic(T + 1);
        int numWhite = 0, numBlack = 0;

        for (int r = 0; r < H; r++)
            for (int c = 0; c < W; c++)
                if (grid[r][c] == Black)
                {
                    numBlack++;
                    dinic.AddEdge(r * W + c, T, 1);
                }
                else if (grid[r][c] == White)
                {
                    numWhite++;
                    dinic.AddEdge(S, r * W + c, 1);
                    for (int d = 0; d < DR.Length; d++)
                    {
                        int nr = r + DR[d], nc = c + DC[d];
                        if (nr >= 0 && nr < H && nc >= 0 && nc < W && grid[nr][nc] == Black)
                            dinic.AddEdge(r * W + c, nr * W + nc, 1);
                    }
                }
        int neighbour = dinic.MaxFlow(S, T);
        int ans = neighbour * 100;
        ans += (Math.Min(numWhite, numBlack) - neighbour) * 10;
        ans += Math.Abs(numWhite - numBlack);

        Console.WriteLine(ans);
    }

    public class Dinic
    {
        public static readonly int INF = (int)1e9;
        public List<Edge>[] G;
        int S, T;
        int[] level;
        int[] iter;

        public Dinic(int V)
        {
            G = new List<Edge>[V];
            for (int i = 0; i < V; i++) G[i] = new List<Edge>();
        }
        public void AddEdge(int from, int to, int capacity)
        {
            G[from].Add(new Edge(to, capacity, G[to].Count));
            G[to].Add(new Edge(from, 0, G[from].Count - 1));
        }
        public int MaxFlow(int s, int t)
        {
            S = s; T = t;
            int flow = 0;
            while (true)
            {
                BFS();
                if (level[T] < 0) return flow;
                iter = new int[G.Length];
                int f;
                while ((f = DFS(S, INF)) > 0) { flow += f; }
            }
        }
        private void BFS()
        {
            level = Enumerable.Repeat(-1, G.Length).ToArray();
            Queue<int> que = new Queue<int>();
            que.Enqueue(S);
            level[S] = 0;
            while (que.Count > 0)
            {
                int v = que.Dequeue();
                foreach (Edge e in G[v])
                    if (e.capacity > 0 && level[e.to] < 0)
                    {
                        level[e.to] = level[v] + 1;
                        que.Enqueue(e.to);
                    }
            }
        }
        private int DFS(int v, int flow)
        {
            if (v == T) return flow;
            while (iter[v] < G[v].Count)
            {
                Edge e = G[v][iter[v]];
                if (e.capacity > 0 && level[v] < level[e.to])
                {
                    int d = DFS(e.to, Math.Min(flow, e.capacity));
                    if (d > 0)
                    {
                        e.capacity -= d;
                        G[e.to][e.reverse].capacity += d;
                        return d;
                    }
                }
                iter[v]++;
            }
            return 0;
        }
    }

    public class Edge
    {
        public int to, reverse;
        public int capacity;
        public Edge(int to, int capacity, int reverse)
        {
            this.to = to;
            this.capacity = capacity;
            this.reverse = reverse;
        }
        public override string ToString()
        {
            return "to:" + to + " cap:" + capacity;
        }
    }

}

class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0