-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11408_열혈강호_5.py
69 lines (54 loc) · 1.73 KB
/
11408_열혈강호_5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
from collections import deque
from collections import defaultdict
INF = 987654321
def input(): return sys.stdin.readline().strip()
(N, M) = map(int, input().split())
edges = [[] for _ in range(N + M + 2)]
for here in range(1, N + 1):
(K, *temp) = map(int, input().split())
for j in range(K):
edges[here].append((N + temp[2*j], 1, temp[2*j+1]))
edges[N + temp[2*j]].append((here, 0, -temp[2*j+1]))
for person in range(1, N + 1):
edges[0].append((person, 1, 0))
edges[person].append((0, 0, 0))
for work in range(N + 1, N + M + 1):
edges[work].append((N + M + 1, 1, 0))
edges[N + M + 1].append((work, 0, 0))
flow = [defaultdict(int) for _ in range(N + M + 2)]
answer = [0, 0]
while True:
distance = [INF] * (N + M + 2)
in_queue = [False] * (N + M + 2)
pre = [-1] * (N + M + 2)
in_queue[0] = True
queue = deque([0])
distance[0] = 0
while queue:
here = queue.popleft()
in_queue[here] = False
for (there, capacity, cost) in edges[here]:
if capacity - flow[here][there] <= 0:
continue
if distance[here] + cost >= distance[there]:
continue
distance[there] = distance[here] + cost
pre[there] = here
if not in_queue[there]:
queue.append(there)
in_queue[there] = True
if pre[N + M + 1] == -1:
break
here = N + M + 1
while here:
flow[pre[here]][here] += 1
flow[here][pre[here]] -= 1
for (there, capacity, cost) in edges[pre[here]]:
if there == here:
answer[1] += cost
break
here = pre[here]
answer[0] += 1
print(answer[0])
print(answer[1])