結果
問題 | No.119 旅行のツアーの問題 |
ユーザー | cologne |
提出日時 | 2022-02-08 23:18:36 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 36 ms / 5,000 ms |
コード長 | 3,349 bytes |
コンパイル時間 | 183 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 11,520 KB |
最終ジャッジ日時 | 2024-06-23 19:08:14 |
合計ジャッジ時間 | 1,745 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 30 ms
11,136 KB |
testcase_01 | AC | 32 ms
11,264 KB |
testcase_02 | AC | 33 ms
11,264 KB |
testcase_03 | AC | 31 ms
11,136 KB |
testcase_04 | AC | 32 ms
11,136 KB |
testcase_05 | AC | 32 ms
11,008 KB |
testcase_06 | AC | 35 ms
11,264 KB |
testcase_07 | AC | 31 ms
11,136 KB |
testcase_08 | AC | 30 ms
11,136 KB |
testcase_09 | AC | 31 ms
11,136 KB |
testcase_10 | AC | 31 ms
11,008 KB |
testcase_11 | AC | 30 ms
11,264 KB |
testcase_12 | AC | 32 ms
11,136 KB |
testcase_13 | AC | 31 ms
11,136 KB |
testcase_14 | AC | 31 ms
11,136 KB |
testcase_15 | AC | 31 ms
11,136 KB |
testcase_16 | AC | 34 ms
11,392 KB |
testcase_17 | AC | 32 ms
11,136 KB |
testcase_18 | AC | 31 ms
11,136 KB |
testcase_19 | AC | 30 ms
11,008 KB |
testcase_20 | AC | 33 ms
11,264 KB |
testcase_21 | AC | 35 ms
11,520 KB |
testcase_22 | AC | 36 ms
11,392 KB |
ソースコード
from collections import deque from math import inf import sys sys.setrecursionlimit(10**6) class MaxFlow: def __init__(self, N: int): self.__N = N self.__G = [[] for _ in range(N)] self.__E = [] def add_edge(self, from_: int, to: int, cap: int) -> int: """ Adds an edge oriented from vertex from_ to vertex to with capacity cap. Returns edge index. """ assert 0 <= from_ < self.__N assert 0 <= to < self.__N assert 0 <= cap from_edge = [to, None, cap] to_edge = [from_, from_edge, 0] from_edge[1] = to_edge self.__E.append((from_edge, to_edge)) self.__G[from_].append(from_edge) self.__G[to].append(to_edge) return len(self.__E) - 1 def get_edge(self, i: int): """ Returns i-th edge state as tuple (from_, to, cap, flow) """ (to, _, flow), (from_, _, remain) = self.__E[i] return (from_, to, flow+remain, flow) def flow(self, source: int, sink: int, flow_limit: int = inf) -> float: """ Returns maximum-flow from vertex source to vertex sink, bounded with flow_limit. """ assert 0 <= source < self.__N assert 0 <= sink < self.__N assert source != sink assert flow_limit >= 0 level = [] cnt = [] def bfs(): level[source] = 0 Q = deque([source]) while len(Q) != 0: u = Q.popleft() for edg in self.__G[u]: to, _, cap = edg if cap > 0 and level[to] == -1: level[to] = level[u] + 1 if to == sink: return True Q.append(to) return False def flow(e, f): if e == sink: return f while cnt[e] < len(self.__G[e]): edg = self.__G[e][cnt[e]] to, rev, cap = edg if cap <= 0 or level[to] <= level[e]: cnt[e] += 1 continue flowed = flow(to, min(f, cap)) if flowed > 0: edg[2] -= flowed rev[2] += flowed return flowed level[e] = -1 return 0 answer = 0 while flow_limit > 0: level = [-1] * self.__N cnt = [0] * self.__N if not bfs(): break while True: flowed = flow(source, flow_limit) if flowed <= 0: break answer += flowed flow_limit -= flowed return answer def main(): N = int(input()) BC = [tuple(map(int, input().split())) for i in range(N)] M = int(input()) DE = [tuple(map(int, input().split())) for i in range(M)] G = MaxFlow(2*N+2) ans = 0 for i in range(N): B, C = BC[i] ans += max(B, C) G.add_edge(2*N, 2*i, max(B, C) - C) G.add_edge(2*i, 2*i+1, max(B, C)) G.add_edge(2*i+1, 2*N+1, max(B, C) - B) for d, e in DE: G.add_edge(2*d+1, 2*e, inf) ans -= G.flow(2*N, 2*N+1) print(ans) if __name__ == '__main__': main()