/* -*- coding: utf-8 -*- * * 2604.cc: No.2604 Initial Motion - yukicoder */ #include #include #include #include #include using namespace std; /* constant */ const int MAX_K = 2000; const int MAX_N = 2000; const int INF = 1 << 30; const long long LINF = 1LL << 62; /* typedef */ using ll = long long; template struct MinCostFlow { typedef pair pii; typedef pair pti; typedef pair ptt; typedef vector vpii; typedef vector vvpii; int n, m; vvpii nbrs; T inf; T max_flow, min_cost; vpii prvs; vector minfs, caps, costs, flows, ds; MinCostFlow(int _n, T _inf): n(_n), m(0), inf(_inf) { nbrs.assign(n, vpii()); minfs.assign(n, 0); prvs.assign(n, pii(-1, -1)); caps.clear(), costs.clear(), flows.clear(); ds.assign(n, 0); max_flow = min_cost = 0; } int addedges(int u, int v, T cap, T cost) { // edge[m]: u -> v, cap=c nbrs[u].push_back(pii(v, m)); caps.push_back(cap); costs.push_back(cost); flows.push_back(0); // edge[m + 1]: v -> u, cap=0 nbrs[v].push_back(pii(u, m + 1)); caps.push_back(0); costs.push_back(-cost); flows.push_back(0); m += 2; return m; } ptt flow(int st, int gl) { // return ptt(max_flow, min_cost) return flow(st, gl, inf, false); } ptt flow(int st, int gl, T limit, bool cont) { // return ptt(max_flow, min_cost) if (! cont) { fill(flows.begin(), flows.end(), 0); max_flow = min_cost = 0; } while (max_flow < limit) { //printf("max_flow = %d, limit = %d\n", max_flow, limit); fill(prvs.begin(), prvs.end(), pii(-1, -1)); fill(ds.begin(), ds.end(), inf); prvs[st] = pii(st, -1); minfs[st] = inf; ds[st] = 0; priority_queue q; q.push(pti(0, st)); while (! q.empty()) { pti u = q.top(); q.pop(); T ud = -u.first; int ui = u.second; if (ud != ds[ui]) continue; if (ui == gl) break; for (auto ve: nbrs[ui]) { int vi = ve.first, ei = ve.second; T vc = caps[ei] - flows[ei]; T vd = ud + costs[ei]; if (vc > 0 && ds[vi] > vd) { prvs[vi] = pii(ui, ei); minfs[vi] = (minfs[ui] < vc) ? minfs[ui] : vc; ds[vi] = vd; q.push(pti(-vd, vi)); } } } if (prvs[gl].first < 0) break; T min_flow = min(minfs[gl], limit - max_flow); for (int j = gl; j != st;) { int i = prvs[j].first, ei = prvs[j].second; flows[ei] += min_flow; flows[ei ^ 1] -= min_flow; j = i; } max_flow += min_flow; min_cost += ds[gl] * min_flow; } return ptt(max_flow, min_cost); } }; /* global variables */ int as[MAX_K], bs[MAX_N]; /* subroutines */ /* main */ int main() { int k, n, m; scanf("%d%d%d", &k, &n, &m); for (int i = 0; i < k; i++) scanf("%d", as + i), as[i]--; for (int i = 0; i < n; i++) scanf("%d", bs + i); MinCostFlow mcf(n + 2, LINF); for (int i = 0; i < m; i++) { int u, v; ll d; scanf("%d%d%lld", &u, &v, &d); u--, v--; mcf.addedges(u, v, INF, d); mcf.addedges(v, u, INF, d); } for (int i = 0; i < k; i++) mcf.addedges(n, as[i], 1, 0); for (int i = 0; i < n; i++) mcf.addedges(i, n + 1, bs[i], 0); auto f = mcf.flow(n, n + 1); printf("%lld\n", f.second); return 0; }