結果

問題 No.904 サメトロ
ユーザー hahho28hahho28
提出日時 2021-04-05 09:26:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,115 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 10,912 KB
実行使用メモリ 71,888 KB
最終ジャッジ日時 2023-08-28 21:13:35
合計ジャッジ時間 13,291 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 224 ms
43,124 KB
testcase_02 AC 234 ms
43,240 KB
testcase_03 AC 224 ms
43,232 KB
testcase_04 AC 229 ms
43,284 KB
testcase_05 AC 213 ms
43,252 KB
testcase_06 AC 222 ms
43,120 KB
testcase_07 AC 225 ms
43,224 KB
testcase_08 AC 232 ms
43,304 KB
testcase_09 AC 222 ms
43,204 KB
testcase_10 AC 227 ms
43,256 KB
testcase_11 AC 225 ms
43,332 KB
testcase_12 AC 220 ms
43,276 KB
testcase_13 AC 226 ms
43,228 KB
testcase_14 AC 228 ms
43,272 KB
testcase_15 AC 218 ms
43,200 KB
testcase_16 AC 237 ms
43,444 KB
testcase_17 AC 221 ms
43,292 KB
testcase_18 AC 218 ms
43,192 KB
testcase_19 AC 227 ms
43,136 KB
testcase_20 AC 221 ms
43,396 KB
testcase_21 AC 225 ms
43,208 KB
testcase_22 AC 228 ms
43,260 KB
testcase_23 AC 224 ms
43,196 KB
testcase_24 AC 224 ms
43,296 KB
testcase_25 AC 219 ms
43,268 KB
testcase_26 AC 226 ms
43,512 KB
testcase_27 AC 232 ms
43,412 KB
testcase_28 AC 222 ms
43,180 KB
testcase_29 AC 223 ms
43,368 KB
testcase_30 AC 221 ms
43,100 KB
testcase_31 AC 220 ms
43,208 KB
testcase_32 AC 222 ms
43,208 KB
testcase_33 AC 225 ms
43,144 KB
testcase_34 AC 219 ms
43,208 KB
testcase_35 AC 228 ms
71,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import numpy as np
from scipy.sparse.csgraph import maximum_flow
from scipy.sparse import csr_matrix
from itertools import product


def bisect(ng, ok, judge, eps=1):
    while abs(ng-ok) > eps:
        m = (ng+ok)//2
        if judge(m):
            ok = m
        else:
            ng = m
    return ok


def solve(ab):
    INF = 10**8
    ab = [(INF,INF)]+ab
    n = len(ab)

    g = np.zeros((2*n+2,2*n+2), dtype=int)

    # 0: start, 1: goal, 2-(n+1): left, (n+2)-(2n-1): right

    for i,(a,b) in enumerate(ab):
        g[0,i+2] = a
        g[i+n+2,1] = b
    for i,j in product(range(n), repeat=2):
        if i == j:
            continue
        g[i+2,j+n+2] = INF

    res = maximum_flow(csr_matrix(g), 0, 1)
    max_t = res.flow_value
    ta = sum(a for a,b in ab[1:])
    tb = sum(b for a,b in ab[1:])

    def judge(t):
        g[0,2] = t-ta
        g[n+2,1] = t-tb
        return maximum_flow(csr_matrix(g), 0, 1).flow_value == t

    min_t = bisect(max(ta,tb)-1, max_t, judge)
    return max_t - min_t + 1



n = int(input())
ab = [tuple(map(int,input().split())) for _ in range(n-1)]
print(solve(ab))
0