結果

問題 No.1479 Matrix Eraser
ユーザー titiatitia
提出日時 2024-05-07 03:42:13
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 2,267 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 558,516 KB
最終ジャッジ日時 2024-05-07 03:42:56
合計ジャッジ時間 22,781 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
59,904 KB
testcase_01 AC 57 ms
54,528 KB
testcase_02 AC 57 ms
63,360 KB
testcase_03 AC 47 ms
55,296 KB
testcase_04 AC 44 ms
55,168 KB
testcase_05 AC 52 ms
62,464 KB
testcase_06 AC 52 ms
62,464 KB
testcase_07 AC 286 ms
124,664 KB
testcase_08 AC 412 ms
159,976 KB
testcase_09 AC 931 ms
278,568 KB
testcase_10 MLE -
testcase_11 AC 1,212 ms
303,560 KB
testcase_12 AC 370 ms
137,736 KB
testcase_13 AC 445 ms
175,344 KB
testcase_14 AC 355 ms
138,844 KB
testcase_15 AC 157 ms
90,528 KB
testcase_16 AC 415 ms
155,512 KB
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 TLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 TLE -
testcase_25 MLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

H,W=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]

# dinic法

from collections import defaultdict,deque

V=H*W*3+2
start=V-2
goal=V-1

EDGE=[defaultdict(int) for i in range(V)]

now=H*W
for i in range(H):
    L=defaultdict(list)
    for j in range(W):
        if A[i][j]!=0:
            L[A[i][j]].append(i*W+j)

    for l in L:
        EDGE[start][now]=1

        for x in L[l]:
            EDGE[now][x]=1<<63

        now+=1

for j in range(W):
    L=defaultdict(list)
    for i in range(H):
        if A[i][j]!=0:
            L[A[i][j]].append(i*W+j)

    for l in L:
        EDGE[now][goal]=1

        for x in L[l]:
            EDGE[x][now]=1<<63

        now+=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]
            

print(ANS)

0