'''
添加一个虚拟源点SS和虚拟汇点TT,
SS 和 所有原图源点间连接容量无穷大的边
原图汇点和TT 连接容量无穷大的边
求SS到TT的最大流
'''
from typing import List
from collections import deque
class FortdFulkerson:
def __init__(self, edges, source_node, end_node, max_node_num, max_edge_num):
self.edges = edges[::]
self.source_node = source_node
self.end_node = end_node
self.max_edge_num = max_edge_num
self.max_node_num = max_node_num
def getMaxFlow(self):
e = [-1] * (self.max_edge_num*2 + 1)
f = [-1] * (self.max_edge_num*2 + 1)
ne = [-1] * (self.max_edge_num*2 + 1)
h = [-1] * (self.max_node_num + 1)
dis = [-1] * (self.max_node_num + 1)
cur = [-1] * (self.max_node_num + 1)
orig_flow = [0] * (self.max_edge_num + 1)
idx = 0
for a, b, w in self.edges:
e[idx], f[idx], ne[idx], h[a] = b, w, h[a], idx
idx += 1
e[idx], f[idx], ne[idx], h[b] = a, 0, h[b], idx
idx += 1
def bfs() -> bool:
for i in range(self.max_node_num + 1):
dis[i] = -1
que = deque()
que.append(self.source_node)
dis[self.source_node] = 0
cur[self.source_node] = h[self.source_node]
while len(que) > 0:
cur_node = que.popleft()
idx = h[cur_node]
while idx != -1:
next_node = e[idx]
if dis[next_node] == -1 and f[idx] > 0:
dis[next_node] = dis[cur_node] + 1
cur[next_node] = h[next_node]
if next_node == self.end_node:
return True
que.append(next_node)
idx = ne[idx]
return False
def dfs(node, limit) -> int:
if node == self.end_node:
return limit
flow = 0
idx = cur[node]
while idx != -1 and flow < limit:
cur[node] = idx
next_node = e[idx]
if dis[next_node] == dis[node]+1 and f[idx] > 0:
t = dfs(next_node, min(f[idx], limit - flow))
if t == 0:
dis[next_node] = -1
f[idx], f[idx^1], flow = f[idx]-t, f[idx^1]+t, flow+t
if self.edges[idx>>1][0] == node:
orig_flow[idx>>1] += t
else:
orig_flow[idx>>1] -= t
idx = ne[idx]
return flow
max_flow = 0
while bfs():
max_flow += dfs(self.source_node, 0x7fffffff)
return max_flow, [(self.edges[i][0], self.edges[i][1], orig_flow[i], self.edges[i][2]) for i in range(len(self.edges))]
n, m, _, _ = map(int, input().split())
s_nodes = list(map(int, input().split()))
t_nodes = list(map(int, input().split()))
SS, TT = n+1, n+2
edges = []
for node in s_nodes:
edges.append((SS, node, 0x7fffffff))
for node in t_nodes:
edges.append((node, TT, 0x7fffffff))
for _ in range(m):
a, b, w = map(int, input().split())
edges.append((a, b, w))
algo = FortdFulkerson(edges, SS, TT, max_node_num=n+2, max_edge_num=len(edges))
ans = algo.getMaxFlow()
print(ans[0])