結果

問題 No.421 しろくろチョコレート
ユーザー むらためむらため
提出日時 2019-02-05 06:28:15
言語 Nim
(2.0.2)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 1,979 bytes
コンパイル時間 2,778 ms
コンパイル使用メモリ 64,476 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 14:57:20
合計ジャッジ時間 4,456 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 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 2 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 2 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 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 4 ms
4,348 KB
testcase_38 AC 6 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 2 ms
4,348 KB
testcase_44 AC 3 ms
4,348 KB
testcase_45 AC 1 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 2 ms
4,348 KB
testcase_48 AC 3 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 3 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 1 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 7 ms
4,348 KB
testcase_61 AC 13 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 1 ms
4,348 KB
testcase_64 AC 2 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)

type BipartiteMatch = seq[seq[int]]
proc initBipartiteMatch(maxSize:int): BipartiteMatch = newSeqWith(maxSize,newSeq[int]())
proc add(B:var BipartiteMatch,src,dst:int) = (B[dst] &= src;B[src] &= dst)
proc bipartiteMatching(B:var BipartiteMatch) : int =
  var match = newSeqWith(B.len,-1)
  var used : seq[bool]
  proc dfs(B:var BipartiteMatch,src:int) : bool =
    # 交互にペアを結んでいく
    used[src] = true
    for dst in B[src]:
      if match[dst] >= 0 :
        if used[match[dst]] : continue
        if not B.dfs(match[dst]) : continue
      match[src] = dst
      match[dst] = src
      return true
    return false
  for src in 0..<B.len:
    if match[src] >= 0 : continue
    used = newSeq[false](B.len)
    if B.dfs(src) : result += 1



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 = initBipartiteMatch(n)
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' : wSum += 1
    else : bSum += 1
    if C[x][y] != 'w' : continue
    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] == '.' : continue
      F.add(encode(x,y),encode(nx,ny))

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