【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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章