标签:open mem time 状态 int The line void efi
传送门
Luogu团队题链接
看到 \(m\) 这么小,马上想到状压 \(\text{DP}\)。
设 \(dp[i][j]\) 表示在前 \(i\) 家商店中已买商品的状态为 \(j\) 的最小花费。
但是有一点小问题,因为在同一家商店买多次物品时,只需要花一次路费,如果总是特判的话,就会比较麻烦,所以我们不妨先把路费全部加上,然后每次枚举一个物品向后转移,最后再把得到的结果和之前不加路费的去取 \(\min\)。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 100 + 10;
const int __ = 16 + 2;
int n, m, d[_], c[_][__], dp[_][1 << __];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int i = 1; i <= n; ++i) {
read(d[i]);
for (rg int j = 1; j <= m; ++j) read(c[i][j]);
}
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
for (rg int i = 1; i <= n; ++i) {
for (rg int j = 0; j < 1 << m; ++j)
dp[i][j] = min(dp[i][j], dp[i - 1][j] + d[i]);
for (rg int j = 0; j < 1 << m; ++j)
for (rg int k = 1; k <= m; ++k)
if ((j >> (k - 1) & 1) ^ 1)
dp[i][j | 1 << (k - 1)] = min(dp[i][j | 1 << (k - 1)], dp[i][j] + c[i][k]);
for (rg int j = 0; j < 1 << m; ++j)
dp[i][j] = min(dp[i][j], dp[i - 1][j]);
}
printf("%d\n", dp[n][(1 << m) - 1]);
return 0;
}
完结撒花 \(qwq\)
标签:open mem time 状态 int The line void efi
原文地址:https://www.cnblogs.com/zsbzsb/p/11746545.html