[lnsyoj2073/luogu5911]PRZ

题意

给定由 \(n\) 个二元组 \((t,w)\) 组成的集合 \(S\) 和常数 \(W\),需要将 \(S\) 分为任意多个非空子集 \(sub_1,sub_2,\cdots,sub_k\),求:

\[\min\{\sum_{i=1}^k \max_{j\in sub_i}\{t_j\}(\sum_{j\in sub_i}w_j \le W)\} \]

sol

数据范围较小,显然状态压缩 DP。
状态比较好想,\(f_{state}\) 表示状态为 \(state\) 时的最小 \(\sum t\) 值。
转移方程根据定义即可:

\[\min_{s0 \subseteq state}\{f_{s0} + \max_{j\in s0}\{t_j\}(\sum_{j\in s0}w_j \le W)\} \]

需要注意的是:

  1. 本题不可以枚举所有集合后再判断子集,而是应直接子集枚举,具体做法为:\(S0=(S0-1) \operatorname{bitand}state\)
  2. 本题需要预处理 \(\max_{j\in s0}\{t_j\}\)\(\sum_{j\in s0}w_j\)

代码

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int N = 20, M = 1 << 16;

int t[N], w[N];
int maxt[M], sumw[M];
int f[M];
int W, n;

int main(){
    scanf("%d%d", &W, &n);
    for (int i = 0; i < n; i ++ ) scanf("%d %d", &t[i], &w[i]);

    for (int state = 1; state < (1 << 16); state ++ )
        for (int i = 0; i < n; i ++ ) 
            if ((state >> i) & 1) maxt[state] = max(maxt[state], t[i]), sumw[state] += w[i];

    memset(f, 0x3f, sizeof f);
    f[0] = 0;

    for (int state = 1; state < (1 << 16); state ++ )
        for (int s0 = state; s0; s0 = (s0 - 1) & state){
            if (sumw[s0] > W) continue;
            f[state] = min(f[state], f[state - s0] + maxt[s0]);
        }

    printf("%d\n", f[(1 << n) - 1]);

    return 0;
}
posted @ 2024-08-08 20:27  是一只小蒟蒻呀  阅读(21)  评论(0编辑  收藏  举报