BraketBN

Think, Thank, Thunk.

【BZOJ4145】[AMPPZ2014]The Prices【状压DP】【背包】

【题目链接】

【tunix的题解】

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 105, maxm = 18, maxs = 1 << 16, inf = 0x3f3f3f3f;

int n, m, dp[maxn][maxs], w[maxn][maxm], d[maxn], bin[maxm];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

int main() {
	bin[0] = 1;
	for(int i = 1; i <= 16; i++) bin[i] = bin[i - 1] << 1;

	n = iread(); m = iread();
	for(int i = 1; i <= n; i++) {
		d[i] = iread();
		for(int j = 1; j <= m; j++) w[i][j] = iread();
	}

	int S = 1 << m;
	for(int s = 0; s < S; s++) dp[0][s] = inf;
	dp[0][0] = 0;
	for(int i = 1; i <= n; i++) {
		for(int s = 0; s < S; s++) dp[i][s] = dp[i - 1][s] + d[i];
		for(int s = 0; s < S; s++) for(int j = 1; j <= m; j++) {
			int ns = s | bin[j - 1];
			if(s != ns) dp[i][ns] = min(dp[i][ns], dp[i][s] + w[i][j]);
		}
		for(int s = 0; s < S; s++) dp[i][s] = min(dp[i][s], dp[i - 1][s]);
	}

	printf("%d\n", dp[n][S - 1]);		
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BraketBN/article/details/51539338
个人分类: 状压DP
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭