結果

問題 No.421 しろくろチョコレート
ユーザー むらためむらため
提出日時 2019-01-31 06:59:48
言語 Nim
(2.0.2)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,024 bytes
コンパイル時間 2,882 ms
コンパイル使用メモリ 64,192 KB
実行使用メモリ 4,412 KB
最終ジャッジ日時 2023-10-24 14:57:13
合計ジャッジ時間 4,619 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

import sequtils
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
template `min=`*(x,y) = x = min(x,y)

# 最大流 O(FE) (F:最大流の流量)
type Edge = tuple[dst,cap,rev:int]
const INF = 1e10.int
type Flow = seq[seq[Edge]]
proc add(F:var Flow,src,dst,cap:int) =
  F[src] &= (dst,cap,F[dst].len)
  F[dst] &= (src,0,F[src].len - 1)
proc fordFullkerson(F:var Flow,src,dst:int) : int =
  var used : seq[bool]
  proc dfs(F:var Flow,src,dst,flow:int) : int =
    if src == dst : return flow
    used[src] = true
    for i,e in F[src]:
      if used[e.dst] or e.cap <= 0 : continue
      let d = F.dfs(e.dst,dst,flow.min(e.cap))
      if d <= 0 : continue
      F[src][i].cap -= d
      F[e.dst][e.rev].cap += d
      return d
  while true:
    used = newSeq[bool](F.len)
    let flow = F.dfs(src,dst,INF)
    if flow == 0 : return
    result += flow


proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" .}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord

let h = scan()
let w = scan()
proc encode(x,y:int):int = x * h + y
let n = h * w
var C = newSeqWith(w,newSeqWith(h,'.'))
for y in 0..<h:
  for x in 0..<w:
    C[x][y] = getchar_unlocked()
  discard getchar_unlocked()
var F : Flow = newSeqWith(n+2,newSeq[Edge]())
var wSum = 0
var bSum = 0
for x in 0..<w:
  for y in 0..<h:
    if C[x][y] == '.' : continue
    const dxdy4 :seq[tuple[x,y:int]] = @[(0,1),(1,0),(0,-1),(-1,0)]
    if C[x][y] == 'w' :
      F.add(encode(x,y),n+1,1)
      wSum += 1
    else :
      F.add(n,encode(x,y),1)
      bSum += 1
    for d in dxdy4:
      let nx = x + d.x
      let ny = y + d.y
      if nx < 0 or ny < 0 or nx >= w or ny >= h : continue
      if C[nx][ny] != 'w' : continue
      F.add(encode(x,y),encode(nx,ny),1)

var ans = F.fordFullkerson(n,n+1)
wSum -= ans
bSum -= ans
ans *= 100
let pair = min(bSum,wSum)
ans = ans + 10 * pair
wSum -= pair
bSum -= pair
echo ans + wSum + bSum
0