1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include <algorithm> #include <iostream> #include <vector> #include <cstdio> #define MAXN 500005 #define MAXM 2000005 #define ll long long #define cjb 1919810114514843 using namespace std; int n, m, q, cnt; int a[MAXN]; ll b[MAXN];
struct edge { int st, to; ll w; }e[MAXM]; bool cmp(edge a, edge b) { return a.w > b.w; }
int fa[MAXM]; int find(int x) { if(x == fa[x]) return x; return fa[x] = find(fa[x]); }
ll dis[MAXM]; vector <int> g[MAXM]; void klskr() { sort(e + 1, e + 1 + m, cmp); for(int i = 1; i <= m; i++) { int u = e[i].st, v = e[i].to; int fu = find(u), fv = find(v); if(fu == fv) continue; cnt++; fa[cnt] = fa[fv] = fa[fu] = cnt; dis[cnt] = e[i].w; g[cnt].push_back(fv); g[cnt].push_back(fu); g[fv].push_back(cnt); g[fu].push_back(cnt); } return; }
int siz[MAXM], dep[MAXM], faz[MAXM]; int top[MAXM], son[MAXM]; void dfs1(int u, int dad) { siz[u] = 1; faz[u] = dad; dep[u] = dep[dad] + 1; for(int v : g[u]) { if(v == dad) continue; dfs1(v, u); siz[u] += siz[v]; if(siz[v] > siz[son[u]]) son[u] = v; } } void dfs2(int u, int tup) { top[u] = tup; if(son[u]) dfs2(son[u], tup); for(int v : g[u]) { if(v == son[u] || v == faz[u]) continue; dfs2(v, v); } } int LCA(int u, int v) { while(top[u] != top[v]) { if(dep[top[u]] > dep[top[v]]) u = faz[top[u]]; else v = faz[top[v]]; } return dep[u] < dep[v] ? u : v; }
int main() { scanf("%d%d%d", &n, &m, &q); cnt = n; for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); fa[i] = i; } for(int i = 1; i <= n; i++) scanf("%lld", &b[i]); for(int i = 1; i <= m; i++) { int u, v; ll w; scanf("%d%d%lld", &u, &v, &w); e[i] = edge{u, v, w}; } for(int i = 1; i <= q; i++) { int u; scanf("%d", &u); e[++m] = edge{u, n+1, cjb}; } ++cnt; klskr(); dfs1(cnt, 0); dfs2(cnt, cnt); ll now = b[a[1]]; if(now < 0) { now = 0; printf("0\n"); } for(int i = 2; i <= n; i++) { int u = a[i-1], v = a[i]; ll res = dis[LCA(u, v)]; if(b[v] > 0) { now = min(now, res) + b[v]; } else { res = min(now, res); ll fk = min(res, abs(b[v])); printf("%lld\n", fk); now = res - min(res, abs(b[v])); } } return 0; }
|