-
Notifications
You must be signed in to change notification settings - Fork 6
/
delivering.py
59 lines (54 loc) · 1.54 KB
/
delivering.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
import sys; input = sys.stdin.readline
from random import choice
from heapq import *
def aug(l):
if vis[l]: return 0
vis[l] = 1
for r in g[l]:
if match[r] == -1 or aug(match[r]): match[r] = l; return 1
return 0
N, M, C = map(int, input().split())
R = {0, *map(int, input().split())}
INF = 10**9
V = 2*N
A = [{} for _ in range(N)]
for _ in range(M): a, b, w = map(int, input().split()); A[a][b] = w
D, P = {}, {}
D[0] = 0
pq = [(0, 0)]
while pq:
dd, vv = heappop(pq)
if dd == D[vv]:
for nn in A[vv]:
if nn not in D or D[nn] >= dd + A[vv][nn]:
if nn not in D: P[nn] = set(); D[nn] = INF
if vv in P[nn]: continue
if D[nn] == (x:=dd+A[vv][nn]): P[nn].add(vv)
else: P[nn] = {vv}
D[nn] = x; heappush(pq, (D[nn], nn))
g = [[] for _ in range(V)]
vis = set()
for i, d in sorted(D.items(), key=lambda x: x[1]):
if i not in P: continue
for k in [*P[i]]:
if k in P: P[i] |= P[k]
for i in P:
if i not in R: continue
for j in P[i]:
if j in R: g[j].append(i+N)
match, mcbm = [-1]*V, 0
free = set(range(N))
nfree = len(free)
for l in list(free):
candidates = [r for r in g[l] if match[r] == -1]
if candidates:
mcbm += 1
free.discard(l)
match[choice(candidates)] = l
for f in free:
vis = [0]*nfree
mcbm += aug(f)
have = [0]*N
for i in range(N):
for j in g[i]: have[i] += 1; have[j-N] += 1
print(sum(i>0 for i in have)-mcbm)