結果
問題 | No.119 旅行のツアーの問題 |
ユーザー | tcltk |
提出日時 | 2021-06-27 17:50:31 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 147 ms / 5,000 ms |
コード長 | 2,389 bytes |
コンパイル時間 | 401 ms |
コンパイル使用メモリ | 82,348 KB |
実行使用メモリ | 89,124 KB |
最終ジャッジ日時 | 2024-06-25 11:53:53 |
合計ジャッジ時間 | 4,513 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 132 ms
87,340 KB |
testcase_01 | AC | 140 ms
88,856 KB |
testcase_02 | AC | 125 ms
87,084 KB |
testcase_03 | AC | 128 ms
87,072 KB |
testcase_04 | AC | 127 ms
87,008 KB |
testcase_05 | AC | 129 ms
87,020 KB |
testcase_06 | AC | 147 ms
89,088 KB |
testcase_07 | AC | 132 ms
87,088 KB |
testcase_08 | AC | 126 ms
87,020 KB |
testcase_09 | AC | 127 ms
87,148 KB |
testcase_10 | AC | 126 ms
86,944 KB |
testcase_11 | AC | 129 ms
87,048 KB |
testcase_12 | AC | 126 ms
86,984 KB |
testcase_13 | AC | 128 ms
86,984 KB |
testcase_14 | AC | 127 ms
86,720 KB |
testcase_15 | AC | 127 ms
87,040 KB |
testcase_16 | AC | 141 ms
89,076 KB |
testcase_17 | AC | 129 ms
87,040 KB |
testcase_18 | AC | 127 ms
87,040 KB |
testcase_19 | AC | 125 ms
87,040 KB |
testcase_20 | AC | 144 ms
89,124 KB |
testcase_21 | AC | 139 ms
89,104 KB |
testcase_22 | AC | 139 ms
88,932 KB |
ソースコード
#!/usr/bin/env python3 # from typing import * import sys import io import math import collections import decimal import itertools import bisect import heapq def input(): return sys.stdin.readline()[:-1] # sys.setrecursionlimit(1000000) # _INPUT = """3 # 10 1 # 1 1 # 1 10 # 2 # 0 1 # 1 2 # """ # sys.stdin = io.StringIO(_INPUT) class Dinic: def __init__(self, n): self.n = n self.links = [[] for _ in range(n)] self.depth = None self.progress = None def add_link(self, _from, to, cap): self.links[_from].append([cap, to, len(self.links[to])]) self.links[to].append([0, _from, len(self.links[_from]) - 1]) def bfs(self, s): depth = [-1] * self.n depth[s] = 0 q = collections.deque([s]) while q: v = q.popleft() for cap, to, rev in self.links[v]: if cap > 0 and depth[to] < 0: depth[to] = depth[v] + 1 q.append(to) self.depth = depth def dfs(self, v, t, flow): if v == t: return flow links_v = self.links[v] for i in range(self.progress[v], len(links_v)): self.progress[v] = i cap, to, rev = link = links_v[i] if cap == 0 or self.depth[v] >= self.depth[to]: continue d = self.dfs(to, t, min(flow, cap)) if d == 0: continue link[0] -= d self.links[to][rev][0] += d return d return 0 def max_flow(self, s, t): flow = 0 while True: self.bfs(s) if self.depth[t] < 0: return flow self.progress = [0] * self.n current_flow = self.dfs(s, t, INF) while current_flow > 0: flow += current_flow current_flow = self.dfs(s, t, INF) INF = 10**10 N = int(input()) dinic = Dinic(2*N+2) total = 0 for i in range(N): b, c = map(int, input().split()) dinic.add_link(0, i+1, c) dinic.add_link(i+1, N+1+i, INF) dinic.add_link(N+1+i, 2*N+1, b) total += b + c M = int(input()) Tour = [set() for _ in range(N)] for _ in range(M): d, e = map(int, input().split()) Tour[d].add(e) dinic.add_link(e+1, N+1+d, INF) flow = dinic.max_flow(0, 2*N+1) score = total - flow print(score)