# 浅浅补充一下吧
博客例题属于权值为点的情况,我这里补充一个权值为边的情况
简单来说就是将边的权值转换为该边所连较深点的权值
>hncpc2022A 一起坐火车
动森王国由 n − 1 条铁路将 n 个城市连城一片,每条铁路每天双向客运总量有一个上限,全国的需求订单由i23ob统一处理.
每天都会有很多小朋友想要组团从a地前往b地游玩,需要从i23ob订票,能够成团前往的前提是订票时刻从a地到b地的最短路线上所有铁路当天剩余客运量都能够容纳该订单的小朋友人数.
i23ob按顺序处理订单,成行的订单会对应扣除每段铁路当天的剩余客运量相应的成团人数.
按顺序给出一天的订单,计算有多少小朋友开心地成行了.
```c++
int n, m;
struct edg {
int v, w;
};
int w[N];
vector<edg> mp[N];
int fa[N], dep[N], sz[N], son[N], top[N], dfn[N], rnk[N], cnt = 0;
void dfs1(int u, int o) {
fa[u] = o;
sz[u] = 1;
dep[u] = dep[o] + 1;
for (edg e: mp[u]) {
if (e.v == o) continue;
w[e.v] = e.w;//将边权值转化为较深点的权值
dfs1(e.v, u);
sz[u] += sz[e.v];
if (!son[u] || sz[son[u]] < sz[e.v]) son[u] = e.v;
}
}
void dfs2(int u, int t) {
top[u] = t;
dfn[u] = ++cnt;
rnk[cnt] = u;
if (!son[u]) return;
dfs2(son[u], t);
for (edg e: mp[u]) {
if (e.v == fa[u] || e.v == son[u]) continue;
dfs2(e.v, e.v);
}
}
int mn[N << 2], lz[N << 2];
inline void pu(int p) {
mn[p] = min(mn[ls], mn[rs]);
}
inline void pd(int p, int l, int r) {
if (lz[p] != 0) {
lz[ls] += lz[p];
lz[rs] += lz[p];
mn[ls] += lz[p];
mn[rs] += lz[p];
lz[p] = 0;
}
}
void build(int p, int l, int r) {
mn[p] = 0x3f3f3f3f, lz[p] = 0;
if (l == r) {
mn[p] = w[rnk[l]];
return;
}
build(ls, l, mid), build(rs, mid + 1, r);
pu(p);
}
inline void upd(int p, int l, int r, int x, int y, int k) {
if (x <= l && r <= y) {
mn[p] += k;
lz[p] += k;
return;
}
pd(p, l, r);
if (x <= mid) upd(ls, l, mid, x, y, k);
if (mid < y) upd(rs, mid + 1, r, x, y, k);
pu(p);
}
inline int inq(int p, int l, int r, int x, int y) {
if (x <= l && r <= y) {
return mn[p];
}
pd(p, l, r);
int res = 0x3f3f3f3f;
if (x <= mid) res = min(res, inq(ls, l, mid, x, y));
if (mid < y) res = min(res, inq(rs, mid + 1, r, x, y));
return res;
}
inline int inqtr(int x, int y) {
int res = 0x3f3f3f3f, tx = top[x], ty = top[y];
while (tx != ty) {
if (dep[tx] >= dep[ty])
res = min(res, inq(1, 1, n, dfn[tx], dfn[x])), x = fa[tx];
else
res = min(res, inq(1, 1, n, dfn[ty], dfn[y])), y = fa[ty];
tx = top[x];
ty = top[y];
}
if (dfn[x] > dfn[y]) swap(x, y);
res = min(res, inq(1, 1, n, dfn[x] + 1, dfn[y]));//最后一步时取不到最高点的权值
return res;
}
inline void updtr(int x, int y, int k) {
int tx = top[x], ty = top[y];
while (tx != ty) {
if (dep[tx] >= dep[ty])
upd(1, 1, n, dfn[tx], dfn[x], k), x = fa[tx];
else
upd(1, 1, n, dfn[ty], dfn[y], k), y = fa[ty];
tx = top[x];
ty = top[y];
}
if (dfn[x] > dfn[y]) swap(x, y);
upd(1, 1, n, dfn[x] + 1, dfn[y], k);
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
while (cin >> n >> m) {
cnt = 0;
for (int i = 0; i <= n; ++i) {
mp[i].clear();
w[i] = fa[i] = sz[i] = top[i] = dfn[i] = rnk[i] = son[i] = dep[i] = 0;
}
for (int i = 1, u, v, w; i < n; ++i) {
cin >> u >> v >> w;
mp[u].emplace_back(edg{v, w});
mp[v].emplace_back(edg{u, w});
}
dfs1(1, 0);
dfs2(1, 1);
build(1, 1, n);
int ans = 0;
for (int i = 1, a, b, k; i <= m; ++i) {
cin >> a >> b >> k;
int tmp = inqtr(a, b);
if (tmp >= k) updtr(a, b, -k), ans += k;
}
cout << ans << endl;
}
}
```