跳到正文

题解

P4510 [CTSC2015] 性别改造计划

Alex_Wei约 12 分钟
原文
文章信息与版本

文章信息

发布
2022年7月15日
收录
2026年10月22日
更新
2026年10月27日
篇幅
约 12 分钟
获赞
2
题目
P4510

版本

  1. v1当前

P4510 [CTSC2015] 性别改造计划

10ln⁡(1+A)10\ln(1 + A) 作为贡献系数相当离谱,因 ln⁡(1+A)<5.3\ln(1 + A) < 5.3,考虑直接枚举该值,设为 II。

注意到每层仅和与它相邻的两层有关,且需要知道血缘链相邻两者为异性的情况数量,故设 fi,j,kf_{i, j, k} 表示当前考虑到第 ii 层,已经出现 jj 对血缘链相邻异性,且第 ii 层每只绵羊的性别状态为 kk。

转移时,钦定当前层所有血缘链上绵羊的性别,转移系数怎么求?对于每组繁衍关系 (u,v,b,d)(u, v, b, d),

  • 若 u,vu, v 均未改变性别,产生 IbIb 贡献。
  • 若 u,vu, v 均改变性别,产生 I⌊bd⌋I\lfloor bd\rfloor 贡献。

每只绵羊只有两种性别可能,故考虑集合划分模型。但其仅能处理额外贡献触发条件为涉及到的所有点状态相同,而若与 SS,TT 相连分别表示雄性或雌性则不满足适用条件。不过在梳理贡献时已经自然地给出了另一种表示方法:将 kk 的定义改为第 ii 层每只绵羊是否改变性别的状态,且与 SS 相连表示保留性别,与 TT 相连表示改变性别。

上述模型相当类似 文理分科 以及 小 M 的作物。

据此,枚举当前层每只绵羊是否改变状态 kk,并令初始转移系数 JJ 取到所有贡献,即 ∑Ib+I⌊bd⌋\sum Ib + I\lfloor bd\rfloor,因为我们希望最大化贡献,即利用集合划分模型最小化扣除的代价。

在此基础上建出网络:

  • 枚举所有当前层涉及到的绵羊 ii,
    • 若 ii 在血缘链上且被钦定改变性别,则 i→Ti\to T 连容量 +∞+\infty 的边表示强制改变性别,且 JJ 减去 cic_i。
    • 若 ii 在血缘链上且被钦定保留性别,则 S→iS\to i 连容量 +∞+\infty 的边表示强制保留性别。
    • 若 ii 不在血缘链上,则 S→iS\to i 连容量为 cic_i 的边表示若割掉这条边使得 ii 与 TT 相连,改变性别,则需要 cic_i 的代价。同理 i→Ti\to T 连容量为 00 的边表示保留性别不需要任何代价。
  • 枚举当前层涉及到的繁衍关系 e=(u,v,b,d)e = (u, v, b, d)。
    • S→e1S\to e_1,e1→u,ve_1\to u, v 分别连容量 IbIb 和 +∞+\infty 的边,表示只要 uu 或 vv 改变性别则需要割掉 S→e1S\to e_1 容量为 IbIb 的边,扣掉它本身对 JJ 产生的 IbIb 的贡献。
    • 同理,u,v→e2u, v\to e_2,e2→Te_2 \to T 分别连容量 +∞+\infty 和 I⌊bd⌋I\lfloor bd\rfloor 的边。

JJ 减去 S→TS\to T 的最大流即真正的贡献系数。再枚举上一层状态 j′j' 和 k′k',有转移式

fi−1,j′,k′+J′→fi,j+popcount(k′⊕k),kf_{i - 1, j', k'} + J'\to f_{i, j + \mathrm{popcount}(k' \oplus k), k}

注意不与血缘链上绵羊通过繁衍关系连通的绵羊不属于任何一层。对于它们之间的繁衍关系,不改变性别最优,贡献为 L=∑IbL = \sum Ib。

最终枚举使得 ln⁡(1+A)=I\ln(1 + A) = I 的 AA,用 L+max⁡k=02K−1fn,A,kL + \max\limits_{k = 0} ^ {2 ^ K - 1} f_{n, A, k} 更新答案。

时间复杂度 O(ln⁡(NK)⋅N2K⋅((M+P)M+P+NK2K))\mathcal{O}(\ln(NK) \cdot N2 ^ K \cdot ((M + P) \sqrt{M + P} + NK2 ^ K)),ln⁡(NK)\ln(NK) 有 1010 倍常数但网络流常数较小。

C++
#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
*/
原文
存档评论4 条抓取于