P4510 [CTSC2015] 性别改造计划

· · 题解

P4510 [CTSC2015] 性别改造计划

注意到每层仅和与它相邻的两层有关,且需要知道血缘链相邻两者为异性的情况数量,故设 $f_{i, j, k}$ 表示当前考虑到第 $i$ 层,已经出现 $j$ 对血缘链相邻异性,且第 $i$ 层每只绵羊的性别状态为 $k$。 转移时,钦定当前层所有血缘链上绵羊的性别,转移系数怎么求?对于每组繁衍关系 $(u, v, b, d)$, - 若 $u, v$ 均未改变性别,产生 $Ib$ 贡献。 - 若 $u, v$ 均改变性别,产生 $I\lfloor bd\rfloor$ 贡献。 每只绵羊只有两种性别可能,故考虑集合划分模型。但其仅能处理额外贡献触发条件为涉及到的所有点状态相同,而若与 $S$,$T$ 相连分别表示雄性或雌性则不满足适用条件。不过在梳理贡献时已经自然地给出了另一种表示方法:将 $k$ 的定义改为第 $i$ 层每只绵羊是否改变性别的状态,且与 $S$ 相连表示保留性别,与 $T$ 相连表示改变性别。 上述模型相当类似 [文理分科](https://www.luogu.com.cn/problem/P4313) 以及 [小 M 的作物](https://www.luogu.com.cn/problem/P1361)。 据此,枚举当前层每只绵羊是否改变状态 $k$,并令初始转移系数 $J$ 取到所有贡献,即 $\sum Ib + I\lfloor bd\rfloor$,因为我们希望最大化贡献,即利用集合划分模型最小化扣除的代价。 在此基础上建出网络: - 枚举所有当前层涉及到的绵羊 $i$, - 若 $i$ 在血缘链上且被钦定改变性别,则 $i\to T$ 连容量 $+\infty$ 的边表示强制改变性别,且 $J$ 减去 $c_i$。 - 若 $i$ 在血缘链上且被钦定保留性别,则 $S\to i$ 连容量 $+\infty$ 的边表示强制保留性别。 - 若 $i$ 不在血缘链上,则 $S\to i$ 连容量为 $c_i$ 的边表示若割掉这条边使得 $i$ 与 $T$ 相连,改变性别,则需要 $c_i$ 的代价。同理 $i\to T$ 连容量为 $0$ 的边表示保留性别不需要任何代价。 - 枚举当前层涉及到的繁衍关系 $e = (u, v, b, d)$。 - $S\to e_1$,$e_1\to u, v$ 分别连容量 $Ib$ 和 $+\infty$ 的边,表示只要 $u$ 或 $v$ 改变性别则需要割掉 $S\to e_1$ 容量为 $Ib$ 的边,扣掉它本身对 $J$ 产生的 $Ib$ 的贡献。 - 同理,$u, v\to e_2$,$e_2 \to T$ 分别连容量 $+\infty$ 和 $I\lfloor bd\rfloor$ 的边。 $J$ 减去 $S\to T$ 的最大流即真正的贡献系数。再枚举上一层状态 $j'$ 和 $k'$,有转移式 $$ f_{i - 1, j', k'} + J'\to f_{i, j + \mathrm{popcount}(k' \oplus k), k} $$ 注意不与血缘链上绵羊通过繁衍关系连通的绵羊不属于任何一层。对于它们之间的繁衍关系,不改变性别最优,贡献为 $L = \sum Ib$。 最终枚举使得 $\ln(1 + A) = I$ 的 $A$,用 $L + \max\limits_{k = 0} ^ {2 ^ K - 1} f_{n, A, k}$ 更新答案。 时间复杂度 $\mathcal{O}(\ln(NK) \cdot N2 ^ K \cdot ((M + P) \sqrt{M + P} + NK2 ^ K))$,$\ln(NK)$ 有 $10$ 倍常数但网络流常数较小。 ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long; inline void cmax(ll &x, ll y) {x = x > y ? x : y;} bool Mbe; constexpr int N = 50 + 5; constexpr int M = 1e3 + 5; constexpr int P = 1e4 + 5; constexpr int inf = 0x1064822E; struct flow { int mx, cnt, hd[P], nxt[P << 4], to[P << 4], limit[P << 4]; void clear() {memset(hd, 0, mx + 1 << 2), cnt = 1, mx = 0;} void add(int u, int v, int w = inf) { mx = max(mx, max(u, v)); nxt[++cnt] = hd[u], hd[u] = cnt, to[cnt] = v, limit[cnt] = w; nxt[++cnt] = hd[v], hd[v] = cnt, to[cnt] = u, limit[cnt] = 0; } int T, dis[P], cur[P]; int dfs(int id, int res = inf) { if(id == T) return res; int flow = 0; for(int i = cur[id]; i && res; i = nxt[i]) { int it = to[i], c = min(res, limit[i]); cur[id] = i; if(dis[id] + 1 == dis[it] && c) { int k = dfs(it, c); flow += k, res -= k, limit[i] -= k, limit[i ^ 1] += k; } } if(!flow) dis[id] = -1; return flow; } int maxflow(int s, int t) { mx = max(mx, t); int flow = 0; T = t; while(1) { queue<int> q; memset(dis, -1, mx + 1 << 2); memcpy(cur, hd, mx + 1 << 2); q.push(s), dis[s] = 0; while(!q.empty()) { int t = q.front(); q.pop(); for(int i = hd[t]; i; i = nxt[i]) if(dis[to[i]] == -1 && limit[i]) dis[to[i]] = dis[t] + 1, q.push(to[i]); } if(dis[t] == -1) return flow; flow += dfs(s, 2e9); } } } g; struct edge { int u, v, ori, rev; } e[P]; ll ans; char s[M]; int n, k, m, p, A; int c[M], fa[M], dep[M], bel[M]; int find(int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);} vector<int> ind[N]; vector<edge> edg[N]; void solve(int ln) { static ll f[M][32], h[M][32]; for(int i = 1; i <= n; i++) { ll tot = 0; int t = m + edg[i].size() * 2 + 1; for(edge it : edg[i]) tot += (it.ori + it.rev) * ln; for(int S = 0; S < 1 << k; S++) { ll coef = tot; g.clear(); for(int it : ind[i]) if(dep[it]) if(S >> bel[it] & 1) coef -= c[it], g.add(it, t); // 强制改变 else g.add(0, it); else g.add(0, it, c[it]); // 保留无代价,无边 add(it, t) -> add(0, it) int node = m; for(edge it : edg[i]) { g.add(0, ++node, it.ori * ln), g.add(node, it.u), g.add(node, it.v); // 只要一个改变就不行 g.add(++node, t, it.rev * ln), g.add(it.u, node), g.add(it.v, node); // 只要一个不改变就不行 } coef -= g.maxflow(0, t); if(i == 1) { h[0][S] = coef; continue; } for(int j = 0; j <= (i - 1) * k; j++) for(int T = 0; T < 1 << k; T++) { int cnt = j + __builtin_popcount(T ^ S); cmax(h[cnt][S], f[j][T] + coef); } } swap(f, h), memset(h, 0xcf, sizeof(h)); } ll res = 0; for(int cnt = 1; cnt <= A; cnt++) if(ln == (int) (10 * log(1 + cnt))) for(int S = 0; S < 1 << k; S++) cmax(res, f[cnt][S]); for(edge it : edg[0]) res += it.ori * ln; ans = max(ans, res); } bool Med; int main() { fprintf(stderr, "%.4lf MB\n", (&Mbe - &Med) / 1048576.0); #ifdef ALEX_WEI freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); #endif ios::sync_with_stdio(0); cin >> n >> k >> m >> p >> s + 1; A = (n - 1) * k; for(int i = 1; i <= m; i++) cin >> c[i], fa[i] = i; for(int i = 0; i < k; i++) for(int j = 1; j <= n; j++) { int ind; cin >> ind; dep[ind] = j, bel[ind] = i; } for(int i = 1; i <= p; i++) { double d; cin >> e[i].u >> e[i].v >> e[i].ori >> d; e[i].rev = e[i].ori * d; int x = find(e[i].u), y = find(e[i].v); if(dep[x]) fa[y] = x; else fa[x] = y; } for(int i = 1; i <= m; i++) ind[dep[find(i)]].push_back(i); for(int i = 1; i <= p; i++) edg[dep[find(e[i].u)]].push_back(e[i]); int bound = 10 * log(1 + A); for(int i = 1; i <= bound; i++) { bool exist = 0; for(int j = 1; j <= A; j++) exist |= i == (int) (10 * log(1 + j)); if(exist) solve(i); } cout << ans << endl; return cerr << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n", 0; } /* 2022/7/15 start coding at 19:15 finish debugging at 20:14 */ ```