結果

問題 No.2573 moving up
ユーザー 👑 rin204rin204
提出日時 2023-12-10 18:11:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 1,626 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 75,980 KB
最終ジャッジ日時 2023-12-10 18:11:33
合計ジャッジ時間 4,287 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
61,988 KB
testcase_01 AC 135 ms
75,672 KB
testcase_02 AC 171 ms
75,816 KB
testcase_03 AC 164 ms
75,828 KB
testcase_04 AC 146 ms
75,972 KB
testcase_05 AC 149 ms
75,828 KB
testcase_06 AC 145 ms
75,828 KB
testcase_07 AC 116 ms
75,972 KB
testcase_08 AC 59 ms
68,388 KB
testcase_09 AC 82 ms
75,980 KB
testcase_10 AC 97 ms
75,848 KB
testcase_11 AC 63 ms
70,452 KB
testcase_12 AC 79 ms
73,280 KB
testcase_13 AC 102 ms
75,828 KB
testcase_14 AC 52 ms
66,296 KB
testcase_15 AC 48 ms
63,568 KB
testcase_16 AC 57 ms
68,372 KB
testcase_17 AC 77 ms
75,712 KB
testcase_18 AC 73 ms
72,512 KB
testcase_19 AC 107 ms
75,712 KB
testcase_20 AC 60 ms
70,604 KB
testcase_21 AC 58 ms
70,440 KB
testcase_22 AC 74 ms
75,724 KB
testcase_23 AC 58 ms
68,388 KB
testcase_24 AC 73 ms
75,584 KB
testcase_25 AC 96 ms
75,712 KB
testcase_26 AC 57 ms
68,384 KB
testcase_27 AC 34 ms
53,460 KB
testcase_28 AC 33 ms
53,460 KB
testcase_29 AC 34 ms
53,460 KB
testcase_30 AC 33 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def Hungarian(A, inf=1 << 60):
    n = len(A) + 1
    m = len(A[0]) + 1
    P = [0] * m
    way = [0] * m
    U = [0] * n
    V = [0] * m

    for i in range(1, n):
        P[0] = i
        minV = [inf] * m
        used = [False] * m
        j0 = 0
        while P[j0] != 0:
            i0 = P[j0]
            j1 = 0
            used[j0] = True
            delta = inf
            for j in range(1, m):
                if used[j]:
                    continue
                if i0 == 0 or j == 0:
                    cur = -U[i0] - V[j]
                else:
                    cur = A[i0 - 1][j - 1] - U[i0] - V[j]
                if cur < minV[j]:
                    minV[j] = cur
                    way[j] = j0
                if minV[j] < delta:
                    delta = minV[j]
                    j1 = j
            for j in range(m):
                if used[j]:
                    U[P[j]] += delta
                    V[j] -= delta
                else:
                    minV[j] -= delta
            j0 = j1
        P[j0] = P[way[j0]]
        while j0 != 0:
            j1 = way[j0]
            P[j0] = P[j1]
            j0 = j1

    ret = [-1] * (n - 1)
    for j in range(1, m):
        if P[j] != 0:
            ret[P[j] - 1] = j - 1

    return -V[0], ret


h, w = map(int, input().split())
ans = 0
A = [[0] * w for _ in range(w)]

for i in range(w):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    ans += x
    l = y - x
    r = y
    for j in range(w):
        if j < l:
            A[i][j] = l - j
        elif j > r:
            A[i][j] = j - r

res = Hungarian(A)
print(res[0] + ans)
0