bzoj4145 [AMPPZ2014]The Prices

Description

你要购买 \(m\) 种物品各一件,一共有 \(n\) 家商店,你到第 \(i\) 家商店的路费为 \(d[i]\) ,在第 \(i\) 家商店购买第 \(j\) 种物品的费用为 \(c[i][j]\) ,求最小总费用。

Input

第一行包含两个正整数 \(n,m(1\le n\le 100,1\le m\le 16)\) ,表示商店数和物品数。

接下来 \(n\) 行,每行第一个正整数 \(d[i] (1<=d[i]<=1000000)\) 表示到第 \(i\) 家商店的路费,接下来 \(m\) 个正整数,依次表示 \(c[i] [j] (1\le c [i] [j]\le 1000000)\)

Output

一个正整数,即最小总费用。

Sample

Sample Input

3 4
5 7 3 7 9
2 1 20 3 2
8 1 20 1 1

Sample Output

16

Solution

状压 \(\mathrm{dp}\)

\(f[i][j]\) 表示现在在第 \(i\) 家店,购买状态为 \(j\) 的最小费用。

转移有

\[f[i][j] = f[i - 1][j]+d[i] \]

\[f[i][j | (1<<k-1)]=max\{ f[i][j]+cost[i][k] \} \]

\[f[i][j]=min(f[i][j],f[i-1][j]) \]

#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
inline int read() {
	int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (!(ch ^ '-')) flag = -1; ch = getchar(); }
	while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * flag;
}
int n, m, c[101][17], d[101], f[101][1 << 17];
#define Min(a, b) a = min(a, b)
int main() {
	n = read(); m = read(); int S = (1 << m) - 1;
	rep(i, 1, n) { d[i] = read(); rep(j, 1, m) c[i][j] = read(); }
	memset(f, 127, sizeof f);
	f[0][0] = 0;
	rep(i, 1, n) {
		rep(j, 0, S) f[i][j] = f[i - 1][j] + d[i];
		rep(k, 1, m) rep(j, 0, S) if (!(j & (1 << k - 1))) Min(f[i][j | (1 << k - 1)], f[i][j] + c[i][k]);
		rep(j, 0, S) Min(f[i][j], f[i - 1][j]);
	}
	printf("%d", f[n][S]);
	return 0;
}

posted @ 2018-03-02 19:23  aziint  阅读(126)  评论(0编辑  收藏  举报
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.