結果

問題 No.904 サメトロ
ユーザー hahho
提出日時 2021-04-05 09:26:42
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,115 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 116,736 KB
最終ジャッジ日時 2024-12-30 09:15:40
合計ジャッジ時間 42,083 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample TLE * 3
other TLE * 33
権限があれば一括ダウンロードができます

ソースコード

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