from collections import deque from sys import stdin K,N,M=map(int, stdin.readline().split()) A=list(map(int, stdin.readline().split())) B=list(map(int, stdin.readline().split())) import heapq from heapq import heappush, heappop class MinCostFlow: """ https://github.com/atcoder/ac-library/blob/master/atcoder/internal_csr.hpp https://github.com/atcoder/ac-library/blob/master/atcoder/mincostflow.hpp https://github.com/atcoder/ac-library/blob/master/document_en/mincostflow.md https://github.com/atcoder/ac-library/blob/master/document_ja/mincostflow.md """ def __init__(self, n): self.n = n self._edges = [] def add_edge(self, fr, to, cap, cost): assert 0 <= fr < self.n assert 0 <= to < self.n assert 0 <= cap assert 0 <= cost self._edges.append(self.edge(fr, to, cap, cost)) return len(self._edges) - 1 class edge: def __init__(self, fr, to, cap, cost): self.fr = fr self.to = to self.cap = cap self.flow = 0 self.cost = cost def get_edge(self, i): assert 0 <= i < len(self._edges) return self._edges[i] def edges(self): return self._edges def flow(self, s, t, flow_limit=1<<60): return self.slope(s, t, flow_limit)[-1] def __csr(self, edges): # Compressed Sparse Row self.start = [0] * (self.n + 1) for fr, _ in edges: self.start[fr + 1] += 1 for i in range(self.n): self.start[i + 1] += self.start[i] counter = self.start.copy() self.elist = [0] * len(edges) for fr, e in edges: self.elist[counter[fr]] = e counter[fr] += 1 class _edge: def __init__(self, to, rev, cap, cost): self.to = to self.rev = rev self.cap = cap self.cost = cost def __g(self): degree = [0] * self.n redge_idx = [0] * self.m elist = [(0, None)] * (2 * self.m) now_elist = 0 for i in range(self.m): e = self._edges[i] self.edge_idx[i] = degree[e.fr] degree[e.fr] += 1 redge_idx[i] = degree[e.to] degree[e.to] += 1 elist[now_elist] = (e.fr, self._edge(e.to, -1, e.cap - e.flow, e.cost)) now_elist += 1 elist[now_elist] = (e.to, self._edge(e.fr, -1, e.flow, -e.cost)) now_elist += 1 self.__csr(elist) for i in range(self.m): e = self._edges[i] self.edge_idx[i] += self.start[e.fr] redge_idx[i] += self.start[e.to] self.elist[self.edge_idx[i]].rev = redge_idx[i] self.elist[redge_idx[i]].rev = self.edge_idx[i] def slope(self, s, t, flow_limit=1<<60): assert 0 <= s < self.n assert 0 <= t < self.n assert s != t self.m = len(self._edges) self.edge_idx = [0] * self.m self.__g() result = self.__slope(s, t, flow_limit) for i in range(self.m): e = self.elist[self.edge_idx[i]] self._edges[i].flow = self._edges[i].cap - e.cap return result def __dual_ref(self, s, t): log = self.n.bit_length() mask = (1<= 0 (all reduced cost are positive) # dist[v] <= (n-1)C dual_v = self.dual[v] dist_v = dist[v] for i in range(self.start[v], self.start[v+1]): e = self.elist[i] if not e.cap: continue # |-dual[e.to] + dual[v]| <= (n-1)C # cost <= C - -(n-1)C + 0 = nC cost = e.cost - self.dual[e.to] + dual_v if dist[e.to] - dist_v > cost: dist_to = dist_v + cost dist[e.to] = dist_to self.prev_e[e.to] = e.rev if dist_to == dist_v: que_min.append(e.to) else: heappush(que, dist_to<= 0 - (n-1)C self.dual[v] -= dist[t] - dist[v] return True def __slope(self, s, t, flow_limit): # variants (C = maxcost): # -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0 # reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge self.dual = [0] * self.n self.prev_e = [0] * self.n flow = 0 cost = 0 prev_cost_per_flow = -1 result = [(0, 0)] while flow < flow_limit: if not self.__dual_ref(s, t): break c = flow_limit - flow v = t while v != s: c = min(c, self.elist[self.elist[self.prev_e[v]].rev].cap) v = self.elist[self.prev_e[v]].to v = t while v != s: e = self.elist[self.prev_e[v]] e.cap += c self.elist[e.rev].cap -= c v = self.elist[self.prev_e[v]].to d = -self.dual[s] flow += c cost += c * d if prev_cost_per_flow == d: result.pop() result.append((flow, cost)) prev_cost_per_flow = d return result graph =MinCostFlow(N+2) for i in range(K): t=A[i] graph.add_edge(0,t,1,0) for i in range(N): t=B[i] if t!=0: graph.add_edge(i+1,N+1,t,0) for i in range(M): u,v,d=map(int, stdin.readline().split()) graph.add_edge(u,v,10**5,d) graph.add_edge(v,u,10**5,d) print(graph.flow(0,N+1,K)[1])