题意
给定由 \(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\) 值。
转移方程根据定义即可:
需要注意的是:
- 本题不可以枚举所有集合后再判断子集,而是应直接子集枚举,具体做法为:\(S0=(S0-1) \operatorname{bitand}state\)
- 本题需要预处理 \(\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;
}
标签:sub,min,max,sum,s0,lnsyoj2073,state,luogu5911,PRZ
From: https://www.cnblogs.com/XiaoJuRuoUP/p/-/lnsyoj2073_luogu5911