#include using namespace std; using ll = long long; using pll = pair; #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 1; template struct Edge{ int to; T w; Edge(int to_, T w_=1){ to = to_; w=w_; } }; template using Tree = vector>>; template using Graph = vector>>; /* 容量&重み付きエッジ for Dinic */ template struct REdge{ int to; T cap; T cost; int rev; REdge(int to_, T cap_, T cost_=1){ to = to_; cap = cap_; cost = cost_; } REdge(int to_, T cap_, T cost_, int rev_){ to = to_; cap = cap_; cost = cost_; rev = rev_; } }; /* 残余グラフ for Dinic */ template using RGraph = vector>>; // ここから template struct MinCostFlow{ vector>> graph; int n; MinCostFlow(RGraph graph_){ n = (int)graph_.size(); graph.resize(n); for(int from=0; from e : graph_[from]){ graph[from].push_back(REdge(e.to, e.cap, e.cost, (int)graph[e.to].size())); graph[e.to].pb(REdge(from, 0, -e.cost, (int)graph[from].size()-1)); } } } //コストが非負実数の場合 T min_cost_flow1(int s, int t, T f){ RGraph rgraph(n); vector h(n, 0); //ポテンシャル vector dist(n, 0); //最短距離 vector prevv(n, 0); // 直前の頂点 vector preve(n, 0); // 直前の辺 for(int from=0; from e : graph[from]) rgraph[from].push_back(e); T res = 0; while(f > 0){ //ダイクストラ法を用いてhを更新 priority_queue, vector>, greater>> PQ; for(int i=0; i p = PQ.top(); PQ.pop(); int v = p.se; if(dist[v] < p.fi) continue; for(int i=0; i<(int)rgraph[v].size(); i++){ REdge &e = rgraph[v][i]; if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){ dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; PQ.push({dist[e.to], e.to}); } } } if(dist[t] == LINF){ //これ以上流せない return -1; } for(int v=0; v &e = rgraph[prevv[v]][preve[v]]; e.cap -= d; rgraph[v][e.rev].cap += d; } } return res; } //負のコストが存在する場合 T min_cost_flow2(int s, int t, T f){ RGraph rgraph(n); vector dist(n, 0); //最短距離 vector prevv(n, 0); // 直前の頂点 vector preve(n, 0); // 直前の辺 for(int from=0; from e : graph[from]) rgraph[from].push_back(e); T res = 0; while(f > 0){ //ベルマンフォード法により, s-t間最短経路を求める for(int i=0; i &e = rgraph[v][i]; if(e.cap > 0 && dist[e.to] > dist[v] + e.cost){ dist[e.to] = dist[v] + e.cost; prevv[e.to] = v; preve[e.to] = i; update = true; } } } } if(dist[t] == LINF){ //これ以上流せない return -1; } //s-t間最短経路に沿って目一杯流す T d = f; for(int v=t; v!=s; v=prevv[v]){ d = min(d, rgraph[prevv[v]][preve[v]].cap); } f -= d; res += d*dist[t]; for(int v=t; v!=s; v=prevv[v]){ REdge &e = rgraph[prevv[v]][preve[v]]; e.cap -= d; rgraph[v][e.rev].cap += d; } } return res; } }; void solve(){ int K, N, M; cin >> K >> N >> M; vector A(K); for(int i=0; i> A[i]; A[i]--; } vector B(N); for(int i=0; i> B[i]; } RGraph G(N+2); int s = N, t = N+1; for(int i=0; i(A[i], 1, 0LL)); for(int i=0; i> u >> v >> d; u--; v--; G[u].pb(REdge(v, LINF, d)); G[v].pb(REdge(u, LINF, d)); } for(int i=0; i(t, B[i], 0LL)); MinCostFlow mcf(G); cout << mcf.min_cost_flow1(s, t, K) << '\n'; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }