#include #define INF 2000000000000001LL typedef struct list { int v; int c; long long d; struct list *n; struct list *rev; } list; void upheap (long long q[][2], int idx) { int pidx = (idx-1)/2; if (idx > 0 && q[pidx][0] > q[idx][0]) { long long s = q[idx][0]; q[idx][0] = q[pidx][0]; q[pidx][0] = s; s = q[idx][1]; q[idx][1] = q[pidx][1]; q[pidx][1] = s; upheap(q, pidx); } return; } void downheap (long long q[][2], int idx, int size) { int chidx = -1; long long ch[2] = { INF, INF }; if (2*idx+1 >= size) { return; } ch[0] = q[2*idx+1][0]; if (2*idx+2 < size) { ch[1] = q[2*idx+2][0]; } if (ch[1] < ch[0] && ch[1] < q[idx][0]) { chidx = 2*idx+2; } else if (ch[0] < q[idx][0]) { chidx = 2*idx+1; } if (chidx > 0) { long long s = q[idx][0]; q[idx][0] = q[chidx][0]; q[chidx][0] = s; s = q[idx][1]; q[idx][1] = q[chidx][1]; q[chidx][1] = s; downheap(q, chidx, size); } return; } void dequeue (long long q[][2], int *size, long long *res) { res[0] = q[0][0]; res[1] = q[0][1]; if (*size > 0) { *size -= 1; q[0][0] = q[*size][0]; q[0][1] = q[*size][1]; downheap(q, 0, *size); } return; } int main () { int k = 0; int n = 0; int m = 0; int a = 0; int b[2000] = {}; int u = 0; int v = 0; long long d = 0LL; int res = 0; long long ans = 0LL; long long ad[2002] = {}; long long td[2002] = {}; int cnt[2000] = {}; list pool[16000] = {}; int used = 0; list *e[2002] = {}; list *prev[2002] = {}; long long q[16000][2] = {}; res = scanf("%d", &k); res = scanf("%d", &n); res = scanf("%d", &m); for (int i = 0; i < k; i++) { res = scanf("%d", &a); cnt[a-1]++; } for (int i = 0; i < n; i++) { res = scanf("%d", b+i); } for (int i = 0; i < m; i++) { res = scanf("%d", &u); res = scanf("%d", &v); res = scanf("%lld", &d); pool[used].v = v; pool[used].c = k; pool[used].d = d; pool[used].n = e[u]; pool[used].rev = pool+(used+1); e[u] = pool+used; used++; pool[used].v = u; pool[used].c = 0; pool[used].d = d*(-1LL); pool[used].n = e[v]; pool[used].rev = pool+(used-1); e[v] = pool+used; used++; pool[used].v = u; pool[used].c = k; pool[used].d = d; pool[used].n = e[v]; pool[used].rev = pool+(used+1); e[v] = pool+used; used++; pool[used].v = v; pool[used].c = 0; pool[used].d = d*(-1LL); pool[used].n = e[u]; pool[used].rev = pool+(used-1); e[u] = pool+used; used++; } for (int i = 0; i < n; i++) { pool[used].v = i+1; pool[used].c = cnt[i]; pool[used].d = 0LL; pool[used].n = e[0]; pool[used].rev = pool+(used+1); e[0] = pool+used; used++; pool[used].v = 0; pool[used].c = 0; pool[used].d = 0LL; pool[used].n = e[i+1]; pool[used].rev = pool+(used-1); e[i+1] = pool+used; used++; pool[used].v = n+1; pool[used].c = b[i]; pool[used].d = 0LL; pool[used].n = e[i+1]; pool[used].rev = pool+(used+1); e[i+1] = pool+used; used++; pool[used].v = i+1; pool[used].c = 0; pool[used].d = 0LL; pool[used].n = e[n+1]; pool[used].rev = pool+(used-1); e[n+1] = pool+used; used++; } while (k > 0) { int qsize = 0; int idx = n+1; for (int i = 0; i < n+2; i++) { td[i] = INF; } td[0] = 0LL; q[qsize][0] = 0LL; q[qsize][1] = 0LL; upheap(q, qsize); qsize++; while (qsize > 0) { long long tmp[2] = {}; dequeue(q, &qsize, tmp); if (tmp[0] == td[(int)(tmp[1])]) { list *l = e[(int)(tmp[1])]; while (l != NULL) { if (l->c > 0 && td[l->v] > tmp[0]+l->d) { td[l->v] = tmp[0]+l->d; prev[l->v] = l; q[qsize][0] = td[l->v]; q[qsize][1] = (long long)(l->v); upheap(q, qsize); qsize++; } l = l->n; } } } while (idx > 0) { list *l = prev[idx]; l->c--; l->rev->c++; idx = l->rev->v; } for (int i = 0; i < n+2; i++) { list *l = e[i]; ad[i] += td[i]; while (l != NULL) { l->d += td[i]-td[l->v]; l = l->n; } } ans += ad[n+1]; k--; } printf("%lld\n", ans); return 0; }