結果

問題 No.421 しろくろチョコレート
ユーザー titiatitia
提出日時 2023-03-27 00:23:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 2,340 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 12,176 KB
実行使用メモリ 11,712 KB
最終ジャッジ日時 2023-10-19 13:56:35
合計ジャッジ時間 7,108 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,348 KB
testcase_01 AC 58 ms
11,280 KB
testcase_02 AC 42 ms
11,144 KB
testcase_03 AC 33 ms
10,400 KB
testcase_04 AC 36 ms
10,900 KB
testcase_05 AC 39 ms
10,560 KB
testcase_06 AC 32 ms
10,376 KB
testcase_07 AC 55 ms
10,844 KB
testcase_08 AC 38 ms
11,180 KB
testcase_09 AC 36 ms
10,440 KB
testcase_10 AC 36 ms
10,508 KB
testcase_11 AC 57 ms
10,872 KB
testcase_12 AC 31 ms
10,328 KB
testcase_13 AC 30 ms
10,328 KB
testcase_14 AC 31 ms
10,328 KB
testcase_15 AC 30 ms
10,328 KB
testcase_16 AC 97 ms
11,540 KB
testcase_17 AC 97 ms
11,556 KB
testcase_18 AC 104 ms
11,484 KB
testcase_19 AC 30 ms
10,328 KB
testcase_20 AC 79 ms
11,240 KB
testcase_21 AC 86 ms
11,432 KB
testcase_22 AC 54 ms
10,868 KB
testcase_23 AC 31 ms
10,328 KB
testcase_24 AC 31 ms
10,328 KB
testcase_25 AC 31 ms
10,328 KB
testcase_26 AC 30 ms
10,328 KB
testcase_27 AC 31 ms
10,328 KB
testcase_28 AC 49 ms
11,388 KB
testcase_29 AC 54 ms
11,484 KB
testcase_30 AC 57 ms
11,392 KB
testcase_31 AC 199 ms
11,712 KB
testcase_32 AC 71 ms
11,488 KB
testcase_33 AC 77 ms
11,488 KB
testcase_34 AC 36 ms
11,320 KB
testcase_35 AC 36 ms
11,332 KB
testcase_36 AC 50 ms
11,020 KB
testcase_37 AC 99 ms
11,480 KB
testcase_38 AC 151 ms
11,644 KB
testcase_39 AC 44 ms
11,168 KB
testcase_40 AC 89 ms
10,928 KB
testcase_41 AC 36 ms
10,480 KB
testcase_42 AC 36 ms
10,480 KB
testcase_43 AC 44 ms
11,096 KB
testcase_44 AC 112 ms
11,192 KB
testcase_45 AC 36 ms
10,828 KB
testcase_46 AC 36 ms
10,708 KB
testcase_47 AC 58 ms
11,252 KB
testcase_48 AC 155 ms
11,432 KB
testcase_49 AC 44 ms
11,208 KB
testcase_50 AC 38 ms
10,888 KB
testcase_51 AC 116 ms
11,188 KB
testcase_52 AC 50 ms
10,860 KB
testcase_53 AC 35 ms
10,800 KB
testcase_54 AC 39 ms
11,240 KB
testcase_55 AC 36 ms
10,492 KB
testcase_56 AC 34 ms
10,488 KB
testcase_57 AC 36 ms
10,656 KB
testcase_58 AC 61 ms
11,252 KB
testcase_59 AC 43 ms
10,656 KB
testcase_60 AC 152 ms
11,680 KB
testcase_61 AC 141 ms
11,668 KB
testcase_62 AC 30 ms
10,336 KB
testcase_63 AC 30 ms
10,336 KB
testcase_64 AC 33 ms
11,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

# dinic法

from collections import defaultdict,deque

N,M=map(int,input().split())
MAP=[input().strip() for i in range(N)]

V=N*M+2

EDGE=[defaultdict(int) for i in range(N*M+2)]

for i in range(N):
    for j in range(M):
        if MAP[i][j]=="w":
            for z,w in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]:
                if 0<=z<N and 0<=w<M and MAP[z][w]=="b":
                    EDGE[i*M+j][z*M+w]=1

start=N*M
goal=N*M+1

for i in range(N):
    for j in range(M):
        if MAP[i][j]=="w":
            EDGE[start][i*M+j]=1
        else:
            EDGE[i*M+j][goal]=1

ANS=0
while True:
    # まずBFSする

    DIS=[-1]*V
    Q=deque([start])
    DIS[start]=0
    EDGE2=[[] for i in range(V)]

    while Q:
        x=Q.popleft()
        for to in EDGE[x]:
            if EDGE[x][to]==0:
                continue
            
            if DIS[to]==-1:
                DIS[to]=DIS[x]+1
                Q.append(to)
                EDGE2[x].append(to)
                
            elif DIS[to]==DIS[x]+1:
                EDGE2[x].append(to)

    if DIS[goal]==-1:
        break

    # BFSしたときのEDGEを使ってDFSする

    MINCOST=[float("inf")]*V
    NOW=start
    ROUTE=[-1]*V
    
    while NOW!=-1: # DFS

        cost=MINCOST[NOW]
        
        if NOW==goal:
            ANS+=cost
            i=goal
            
            while i!=start: # goalからたどり,Routeを使ってEDGEの更新
                j=ROUTE[i]
                if EDGE[j][i]==cost:
                    NOW=j
                    EDGE2[j].pop()
                
                EDGE[j][i]-=cost # 使ったルートをいけなく更新
                MINCOST[j]-=cost                
                EDGE[i][j]+=cost # 逆向きに進めるようにする.
                i=j
            continue

        if EDGE2[NOW]:
            to=EDGE2[NOW][-1]
            ROUTE[to]=NOW
            MINCOST[to]=min(cost,EDGE[NOW][to])
            NOW=to

        else:
            if NOW==start:
                break
            EDGE2[ROUTE[NOW]].pop()
            NOW=ROUTE[NOW]
            

ws=0
bs=0

for i in range(N):
    for j in range(M):
        if MAP[i][j]=="w":
            ws+=1
        elif MAP[i][j]=="b":
            bs+=1

ws-=ANS
bs-=ANS

k=min(ws,bs)

print(ANS*100+k*10+(ws+bs-k*2))
0