#include // #include using namespace std; #if __cplusplus >= 202002L using namespace numbers; #endif template struct weighted_flow_network{ struct E{ int from, to; T capacity, flow; C cost; }; vector> adj; vector edge; int n; C cost = 0; weighted_flow_network(int n): n(n), adj(n){ } void clear_flow(){ for(auto &e: edge) e.flow = 0; cost = 0; } int insert(int from, int to, T forward_cap, T backward_cap, C cost){ assert(0 <= from && from < n && 0 <= to && to < n); int ind = (int)edge.size(); adj[from].push_back((int)edge.size()); edge.push_back({from, to, forward_cap, 0, cost}); adj[to].push_back((int)edge.size()); edge.push_back({to, from, backward_cap, 0, -cost}); return ind; } void add_flow(int i, T f){ edge[i].flow += f; cost += f * edge[i].cost; edge[i ^ 1].flow -= f; } friend ostream &operator<<(ostream &out, const weighted_flow_network &F){ out << "\n"; for(auto &e: F.edge){ out << "{" << e.from << " -> " << e.to << ", " << e.cost << ", " << e.flow << "/" << e.capacity << "}\n"; } return out; } }; // Requires weighted_flow_network template struct minimum_cost_maximum_flow_spfa{ static constexpr T eps = (T) 1e-9; weighted_flow_network &F; minimum_cost_maximum_flow_spfa(weighted_flow_network &F): F(F), d(F.n), in_queue(F.n), pe(F.n), state(F.n){ } // type 0: augment as long as a path exists // type 1: augment as long as a negative cost path exists vector d; vector in_queue, q, pe; T expath(int source, int sink, bool type = false){ fill(d.begin(), d.end(), numeric_limits::max()); q = {source}; d[source] = 0; in_queue[source] = true; int beg = 0; bool found = false; while(beg < (int)q.size()){ int u = q[beg ++]; if(u == sink) found = true; in_queue[u] = false; for(auto id: F.adj[u]){ auto &e = F.edge[id]; if(e.capacity - e.flow > eps && d[u] + e.cost < d[e.to]){ d[e.to] = d[u] + e.cost; pe[e.to] = id; if(!in_queue[e.to]){ q.push_back(e.to); in_queue[e.to] = true; } } } } if(found){ T push = numeric_limits::max(); int u = sink; while(u != source){ auto &e = F.edge[pe[u]]; push = min(push, e.capacity - e.flow); u = e.from; } u = sink; assert(push >= 0); if(type && d[sink] >= 0) return false; while(u != source){ F.add_flow(pe[u], push); u = F.edge[pe[u]].from; } return push; } return 0; } vector stack, state; bool try_cycle_cancelling(){ fill(d.begin(), d.end(), 0); q.resize(F.n); iota(q.begin(), q.end(), 0); fill(in_queue.begin(), in_queue.end(), false); fill(pe.begin(), pe.end(), -1); int beg = 0, iter = 0; auto detect_cycle = [&]()->bool{ stack.clear(); fill(state.begin(), state.end(), 0); for(auto s = 0; s < F.n; ++ s){ if(state[s]) continue; for(auto u = s; ; ){ if(state[u]){ if(state[u] == 1){ stack.erase(stack.begin(), find(stack.begin(), stack.end(), u)); assert(!stack.empty() && stack[0] == u); T flow = numeric_limits::max(); for(auto u: stack){ auto &e = F.edge[pe[u]]; flow = min(flow, e.capacity - e.flow); } for(auto u: stack) F.add_flow(pe[u], flow); return true; } break; } stack.push_back(u); state[u] = 1; if(!~pe[u]) break; u = F.edge[pe[u]].from; } for(auto u: stack) state[u] = 2; stack.clear(); } return false; }; while(beg < (int)q.size()){ int u = q[beg ++]; in_queue[u] = false; for(auto id: F.adj[u]){ auto &e = F.edge[id]; if(e.capacity - e.flow > eps && d[u] + e.cost < d[e.to]){ d[e.to] = d[u] + e.cost; pe[e.to] = id; if(++ iter == F.n){ iter = 0; if(detect_cycle()) return true; } if(!in_queue[e.to]){ q.push_back(e.to); in_queue[e.to] = true; } } } } return detect_cycle(); } // type 0: min cost max flow // type 1: min cost flow // O(Augmenting_Paths * SPFA), additional O(SPFA * Cycle_cnt) if negative cycle exists pair solve(int source, int sink, bool type = false, bool negative_cycle_presence = false){ F.clear_flow(); if(negative_cycle_presence) while(try_cycle_cancelling()); T flow = 0; for(T delta; delta = expath(source, sink, type); flow += delta); return {flow, F.cost}; } }; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); int np, n, m; cin >> np >> n >> m; weighted_flow_network F(n + 2); int source = n, sink = n + 1; for(auto i = 0; i < np; ++ i){ int p; cin >> p, -- p; F.insert(source, p, 1, 0, 0); } for(auto u = 0; u < n; ++ u){ int cap; cin >> cap; F.insert(u, sink, cap, 0, 0); } for(auto i = 0; i < m; ++ i){ int u, v; long long d; cin >> u >> v >> d, -- u, -- v; F.insert(u, v, np, 0, d); F.insert(v, u, np, 0, d); } cout << minimum_cost_maximum_flow_spfa(F).solve(source, sink).second << "\n"; return 0; } /* */