#include using namespace std; using ll = long long; using ull = unsigned long long; //using uLL = unsigned __int128; using PT = priority_queue, vector>, greater>>; using PPQ = priority_queue, vector>, greater>>; using PQ = priority_queue, greater>; using P = pair; using vvvvl = vector>>>; using vvvi = vector>>; using vvvl = vector>>; using vvvc = vector>>; using vvvd = vector>>; using vvi = vector>; using vvl = vector>; using vvs = vector>; using vvc = vector>; using vvp = vector>>; using vvb = vector>; using vvd = vector>; using vp = vector>; using vi = vector; using vl = vector; using vu = vector; using vs = vector; using vc = vector; using vb = vector; using vd = vector; #define vvvm vector>> #define vvm vector> #define vm vector #define umap unordered_map #define uset unordered_set #define rrr(l, r) mt()%(r-l+1)+l #define rep(i, s, f) for(long long i = s; i <= f; i++) #define rep1(i, s, f) for(long long i = s; i <= f; i++) #define per(i, s, f) for(long long i = s; i >= f; i--) #define per1(i, s, f) for(long long i = s; i >= f; i--) #define all0(x) (x).begin() ,(x).end() #define all(x) (x).begin() + 1, (x).end() #define ENDL '\n' //////////////////////////////////////////////////////////////////////////////////////////////////////////// //これが本当の組み込み関数ってね(笑) template T or_less(vector &A, T x) { //x以下で最大要素の添字 前提: sort済み 存在しない: -1 return distance(A.begin(), upper_bound(A.begin(), A.end(), x)-1); } template T under(vector &A, T x) { //x未満の最大要素の添字 前提: sort済み 存在しない: -1 return distance(A.begin(), lower_bound(A.begin(), A.end(), x)-1); } template T or_more(vector &A, T x) { //x以上で最小要素の添字  前提: sort済み 存在しない: 配列のサイズ . //distanceのA.beginは添字を出すために常にA.begin() NG: A.begin() + 1 return distance(A.begin(), lower_bound(A.begin(), A.end(), x)); } template T over(vector &A, T x) { //xより大きい最小要素の添字前提: sort済み 存在しない: 配列のサイズ return distance(A.begin(), upper_bound(A.begin(), A.end(), x)); } template vector vec_shift(vector A, ll step, ll dir, ll indexed) {//dir = 1 : 右シフト dir = -1 : 左シフト ll N = A.size() - indexed; vector res(N+1); rep(i, indexed, N) { ll idx = i - step * dir; if(idx < indexed) idx += N; if(idx > N) idx -= N; res.at(i) = A.at(idx); } return res; } template void UNIQUE(vector &A, int indexed) { sort(A.begin() + indexed, A.end()); A.erase(unique(A.begin() + indexed, A.end()), A.end()); } template vector RLE(vector &A, int indexed) { vector res(indexed, -INT_MAX); for(int i = indexed; i < int(A.size()); i++) { if(i == indexed) res.push_back(A[i]); else if(A[i-1] != A[i]) res.push_back(A[i]); } return res; } template void rev90(vector> &A, int indexed) { reverse(A.begin() + indexed, A.end()); int n = A.size(); rep(i, indexed, n-1) { rep(j, i+1, n-1) { swap(A.at(i).at(j), A.at(j).at(i)); } } } int msb(long long a) { if(a == 0) return -1; return 64 - int(__builtin_clzll(a)); } template void chmin(T &a, T b) { a = min(a, b); } template void chmax(T &a, T b) { a = max(a, b); } ////////////////////////////////////////////////////////////////////// //数学系 /////////////////////////////////////////////////////////////////////// ll round(ll x, ll i) {return ll(x + 5 * powl(10, i-1))/ll(powl(10, i)) * ll(powl(10, i));} vp insu_bunkai(ll N) { vp res; for (ll i = 2; i * i <= N; i++) { ll cnt = 0; while(N % i == 0) { cnt++; N /= i; } if(cnt != 0) res.push_back(P(i, cnt)); } if(N != 1) res.push_back(P(N, 1)); return res; } ll extgcd (ll a, ll b, ll &x, ll &y) { if(b == 0) { x = 1;y = 0;return a;} ll d = extgcd(b, a%b, y, x); y -= a/b * x; return d; } template T ceil(T a, T b) { assert(b != 0); if(a % b == 0) return a / b; if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b + 1; else return a / b; } template T floor(T a, T b) { assert(b != 0); if(a % b == 0) return a / b; if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b; else return a/b - 1; } long long Calnum(long long l, long long r, long long M, long long m) {//[l, r]で、modMがmである数の個数 l -= m; r -= m;//並行移 l = ceil(l, M) * M; r = floor(r, M) * M; if(l > r) return 0LL; return (r - l)/M+1; } ll modpow(ll x, ll y, ll mod) { if(x > mod) x %= mod; if(y == 0) return 1; ll res = modpow(x, y >> 1, mod); res = res * res % mod; if(y & 1) res *= x, res %= mod; return res; } ll sqrt_(ll a) { ll l = 0; ll r = 3037000499LL; while(l < r) { ll mid = (l + r + 1) / 2; if(mid * mid <= a) l = mid; else r = mid - 1; } return l; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //グローバル変数を置くところ(情報工学意識高め) //#pragma GCC optimize("O3") //#pragma GCC optimize("unroll-loops") const ll int_max = 1001001001; const ll ll_max = 1001001001001001001LL; const double pi = 3.141592653589793; vl dx{0, 1, 0, -1, 0, 1, 1, -1, -1}; // 座標平面において、(番兵) → ↓ ← ↑ ※ 右から時計回り 注 : グリッド or 座標で上下は反転する。 vl dy{0, 0, -1, 0, 1, 1, -1, -1, 1}; //const ll mod = 1000000007; //const ll mod = 998244353; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// struct Edge { int to; ll cap; int rev; ll cost; }; struct MinCostFlow { vector> G; int N; vl h; vl dist; vi prevV;//ダイクストラは再帰関数的に書けないので、保存する必要がある : 最後に流した分減算するのに使う。 vi prevE;//同上。 ll now_ryuuryou = 0; ll now_cost = 0; vector> es;//辺の情報 [from, EDGE] vector> getid; MinCostFlow(int _N) { N = _N; G.resize(N+1); h.assign(N+1, 0); dist.assign(N+1, ll_max); prevV.resize(N+1); prevE.resize(N+1); es.resize(1); getid.resize(N+1); } private: ll Daiku(int start, int goal, ll &ryuuryou) { ll res = 0; while(ryuuryou > 0) { priority_queue, vector>, greater>> que; dist.assign(N+1, ll_max); dist.at(start) = 0; que.push(P(0, start)); while(!que.empty()) { ll cd = que.top().first; int cp = que.top().second; que.pop(); if(dist[cp] < cd) { continue; } for (int i = 0; i < G.at(cp).size(); i++) { Edge e = G.at(cp).at(i); if(e.cap > 0 && dist[e.to] > dist[cp] + e.cost - (h[e.to] - h[cp])) { //1集目のダイクストラの時、hの中身は0なので最初からこの書き方で不都合は生じない dist[e.to] = dist[cp] + e.cost - (h[e.to] - h[cp]); prevV.at(e.to) = cp; prevE.at(e.to) = i; que.push(P(dist.at(e.to), e.to)); } } } if(dist[goal] == ll_max) { return -1;//辿り着けなかった } for (int i = 1; i <= N; i++) { h[i] += dist[i];//ポテンシャルの更新 逆辺を張る前にやる(負の辺を消すのが目的なので) } ll flow = ryuuryou; for (int v = goal; v != start; v = prevV.at(v)) { flow = min(flow, G[prevV[v]][prevE[v]].cap);//どれだけ流せる } ryuuryou -= flow; now_ryuuryou += flow; res += h[goal] * flow;//大いなる注意点: distに入っているのはポテンシャル付きの距離 hに入っているのは元のグラフでの距離 now_cost += h[goal] * flow; for (int v = goal; v != start; v = prevV[v]) { Edge &e = G[prevV[v]][prevE[v]]; e.cap -= flow; G[v][e.rev].cap += flow; } } return res; } public: void add_edge(int from, int to, ll cap, ll cost) { int Current_Ga_idx = G.at(from).size(); int Current_Gb_idx = G.at(to).size(); G.at(from).push_back(Edge{to, cap, Current_Gb_idx, cost}); G.at(to).push_back(Edge{from, 0, Current_Ga_idx, -1 * cost});//revは、逆向きの辺の添え字 流した時、残余グラフの容量を増やす為 es.emplace_back(from, Edge{to, cap, Current_Gb_idx, cost}); getid[from][to] = es.size() - 1; } void init(int s) {//負の辺がある場合のみ行う。 なお、辿る辺の本数が固定ならば最初からcostに定数を加算して最後に引けば良い vl d(N+1, ll_max); d[s] = 0; rep(i,1,N) {//|頂点数| - 1回ループすれば良い。 rep(j, 0, N) { for(auto e : G[j]) { if(e.cap != 0) d[e.to] = min(d[e.to], d[j] + e.cost);//cap = 0は辺が無いのと同義 : 沼ポイント } } } rep(i,1,N)h[i] = d[i]; }// O(EV) int get_edgecount() {return es.size() - 1;}//辺の総数を取得、O(1) int get_id(int s, int t) {return getid[s][t];}//s→tの辺のidを取得、O(1) ll get_flow(int id) {//番号idの辺の、現状の流量を返す。 auto [_, e] = es[id]; return G[e.to][e.rev].cap; }//O(1) pair get_st(int id) {//番号idの辺のs→tを取得 auto [s, e] = es[id]; return make_pair(s, e.to); }//O(1) void change_edge(int id, ll new_cap, ll new_flow) {//流量変えたくないなら、new_flow = get_flow(id) assert(new_cap >= new_flow); auto[s, e] = es[id]; G[s][G[e.to][e.rev].rev] = Edge{e.to, new_cap-new_flow, e.rev, e.cost}; G[e.to][e.rev] = Edge{s, new_flow, G[e.to][e.rev].rev, -e.cost}; }//O(1) P min_cost(int s, int t, ll Totalryuuryou) {//{コスト、実際に流れた流量} ll ryuuryou = Totalryuuryou - now_ryuuryou; Daiku(s, t, ryuuryou); return P(now_cost, now_ryuuryou); } //O(FElog(V)) or O(FV^2) }; void solve() { ll K, N, M; cin >> K >> N >> M; vvl dist(N+1, vl(N+1, ll_max)); rep(i,1,N) dist[i][i] = 0; vl A(K+1), B(N+1); rep(i,1,K)cin >> A[i]; rep(i,1,N)cin >> B[i]; vvp G(N+1); rep(i,1,M) { ll u, v, d; cin >> u >> v >> d; G[u].emplace_back(d, v); G[v].emplace_back(d, u); } /* rep(i,1,N) { PPQ que; que.push(P(0, i)); while(!que.empty()) { auto [c, now] = que.top(); que.pop(); if(dist[i][now] < c) continue; for(auto [cos, to] : G[now]) { if(dist[i][to] < cos + c) continue; dist[i][to] = cos + c; que.push(P(cos + c, to)); } } } MinCostFlow flow(K + N + 1); ll s = 0; ll t = K + N + 1; rep(i,1,K) { flow.add_edge(0,i,1,0); rep(j, 1, N) { flow.add_edge(i, K + j, 1, dist[A[i]][j]); } } rep(i,1,N) { flow.add_edge(K+i,t,B[i],0); } ll ans = flow.min_cost(s, t, K).first; cout << ans << ENDL; */ MinCostFlow flow(N+1); ll s = 0; ll t = N + 1; rep(i,1,K) { flow.add_edge(0, A[i], 1, 0); } rep(i,1,N) { flow.add_edge(i, t, B[i], 0); } rep(i,1,N) { for(auto [c, to] : G[i]) { flow.add_edge(i, to, K+1, c); flow.add_edge(to, i, K+1, c); } } cout << flow.min_cost(s, t, K).first << ENDL; } //無闇にumapを使わない(特に平方分割時、必要ない事が多い(想定解が平方分割ならば)) //データを分割していく処理では、空配列は作らない方が良い(計算量爆発の温床) //文字列を0-indexedで扱う時、全体の文字数をどう扱うかは最初に決める&忘れない //場合分けが沢山の時はreturn忘れを確認 //LISは連続とは限らない。 //mintでも0で割っては行けない(例: 関数の合成とかでやりがち assert落ちする) //負の数を/2してはいけない(それはfloorでないから) //DPを書く時は、定義を常に明確にする!定義を変えたいと思ったならば、定義が定まるまでは"必要な情報"を考える。 //尺取り法的な事をする時は、rは勿論"lもNを超え無いようにする"事!(違反するとエラーコード139が出たりする) //無断で0を平方数にカウントする人もいる //”部分文字列”と”連続部分文字列”は違うので確認すること //一般のグラフと、有向辺かつその貼り方に制約がある(多くの場合:番号がで解放に伸びる)はだいぶ違うので確認すること //座標を2で割った時の”切り捨て側(左側)”を求めるには 誤:(x / 2) マイナスの時!!! 正:floor(x, 2); //stringでの数字の下から1桁目は 正:S.at(N-1) 誤:S.at(0) //if(S.at(i) == 1) ← charなのに1...? // modは取りましたか...?(´・ω・`) //sortの比較関数は、 a == b ならば falseを返す必要がある(そうで無いとRE(発生しない場合もある)) int main() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); ll T = 1; //cin >> T; rep(i, 1, T) { solve(); } return 0; }