結果

問題 No.1014 competitive fighting
ユーザー maspy
提出日時 2020-03-21 01:59:07
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,830 ms / 2,000 ms
コード長 1,984 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 144,296 KB
最終ジャッジ日時 2024-07-07 12:58:12
合計ジャッジ時間 76,924 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components

N = int(readline())
ABC = np.array(read().split(), np.int64)
A = ABC[::3]
B = ABC[1::3]
C = ABC[2::3]
sort_key = (A << 32) + (B - C)
ind = np.concatenate([np.argsort(sort_key), [N]])
nums = np.concatenate([[-10**10], A])
nums.sort()

fr = []
to = []
cost = []
fr.append(ind[0:-2])
to.append(ind[1:-1])
fr.append([N])
to.append([ind[0]])
cost.append(np.zeros(N, np.int64))
fr.append(ind[np.searchsorted(nums, B - C, side='right') - 2])
to.append(np.arange(N))
cost.append(B)

fr = np.concatenate(fr)
to = np.concatenate(to)
cost = np.concatenate(cost)
G = csr_matrix((np.ones_like(fr), (fr, to)), (N + 1, N + 1))
n_comp, comp = connected_components(G, directed=True, connection='strong')
comp = comp.tolist()

INF = 10 ** 18
fr = fr.tolist()
to = to.tolist()
cost = cost.tolist()
dp = [0] * (n_comp)
G = [[] for _ in range(n_comp)]
self_loop = [0] * n_comp
in_deg = [0] * n_comp
for v, w, c in zip(fr, to, cost):
    cv = comp[v]
    cw = comp[w]
    if cv == cw:
        if c == 0:
            continue
        if self_loop[cv]:
            self_loop[cv] = INF
        else:
            self_loop[cv] = c
    else:
        G[cv].append((cw, c))
        in_deg[cw] += 1

deg_0 = [i for i, x in enumerate(in_deg) if not x]
dp = [0] * (n_comp)
while deg_0:
    v = deg_0.pop()
    dp[v] += self_loop[v]
    for w, c in G[v]:
        x = dp[v] + c
        if dp[w] < x:
            dp[w] = x
        in_deg[w] -= 1
        if not in_deg[w]:
            deg_0.append(w)

answer = [None] * N
for v, w, c in zip(fr, to, cost):
    cv = comp[v]
    cw = comp[w]
    if c == 0:
        continue
    if cv == cw:
        x = dp[cv]
    else:
        x = dp[cv] + c
    answer[w] = str(x) if x < INF else 'BAN'

print('\n'.join(map(str, answer)))
0