結果

問題 No.421 しろくろチョコレート
ユーザー ikd
提出日時 2018-02-09 11:21:08
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 861 ms
コンパイル使用メモリ 107,904 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-12 23:48:41
合計ジャッジ時間 2,705 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

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