結果

問題 No.421 しろくろチョコレート
ユーザー ikdikd
提出日時 2018-02-09 11:21:08
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 96,300 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-03 18:38:14
合計ジャッジ時間 4,119 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

class maxFlow{
  import std.typecons, std.conv, std.algorithm; // , std.stdio;
  alias T=int;
  alias Edge=Tuple!(int, "to", int, "rev", T, "cap");
  Edge[][] g;
  bool[] vis;
  auto inf=(1_000_000_000).to!(T);

  this(int sz){
    g.length=sz;
    vis.length=sz;
  }

  void addEdge(int from, int to, T cap){
    g[from]~=Edge(to, (g[to].length).to!(int), cap);
    g[to]~=Edge(from, (g[from].length-1).to!(int), (0).to!(T));
  }

  T flow(int i, T curf, int sink){
    if(i==sink) return curf;
    vis[i]=true;
    foreach(ref e; g[i]){
      if(vis[e.to] || e.cap==0) continue;
      auto tmpf=flow(e.to, min(curf, e.cap), sink);
      if(tmpf>0){
        e.cap-=tmpf;
        g[e.to][e.rev].cap+=tmpf;
        return tmpf;
      }
    }
    return 0;
  }

  T ford(int source, int sink){
    auto maxf=(0).to!(T);
    while(true){
      fill(vis, false);
      auto f=flow(source, inf, sink);
      if(f>0) maxf+=f;
      else return maxf;
    }
  }
}

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.math;

  int n, m; rd(n, m);
  auto c=new char[][](n, m);
  foreach(i; 0..n) c[i]=readln.chomp.to!(char[]);

  auto dir=[[1, 0], [-1, 0], [0, 1], [0, -1]];
  auto mf=new maxFlow(n*m+2),
       s=n*m, t=s+1;
  int bk=0, wh=0;
  foreach(i; 0..n)foreach(j; 0..m){
    if(c[i][j]=='.') continue;
    if((i+j)&1){
      bk++;
      mf.addEdge(s, i*m+j, 1);
      foreach(d; dir){
        auto ni=i+d[0], nj=j+d[1];
        if(ni<0 || ni>=n || nj<0 || nj>=m) continue;
        if(c[ni][nj]=='.') continue;
        mf.addEdge(i*m+j, ni*m+nj, 1);
      }
    }else{
      wh++;
      mf.addEdge(i*m+j, t, 1);
    }
  }

  auto fmax=mf.ford(s, t);
  bk-=fmax; wh-=fmax;
  writeln(fmax*100+min(bk, wh)*10+(bk-wh).abs);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}
0