結果
| 問題 |
No.421 しろくろチョコレート
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-01-31 06:54:51 |
| 言語 | Nim (2.2.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,023 bytes |
| コンパイル時間 | 2,794 ms |
| コンパイル使用メモリ | 64,520 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-01 11:14:09 |
| 合計ジャッジ時間 | 5,135 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 36 WA * 29 |
ソースコード
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)
wSum += 1
else :
F.add(n+1,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] == '.' : continue
F.add(encode(x,y),encode(nx,ny),1)
var ans = F.fordFullkerson(n+1,n)
wSum -= ans
bSum -= ans
ans *= 100
let pair = min(bSum,wSum)
ans = ans + 10 * pair
wSum -= pair
bSum -= pair
echo ans + wSum + bSum