9
28
2015
0

4145: [AMPPZ2014]The Prices

4145: [AMPPZ2014]The Prices

Time Limit: 20 Sec  Memory Limit: 256 MB
Submit: 175  Solved: 117
[Submit][Status][Discuss]

Description

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

 

Input

第一行包含两个正整数n,m(1<=n<=100,1<=m<=16),表示商店数和物品数。
接下来n行,每行第一个正整数d[i](1<=d[i]<=1000000)表示到第i家商店的路费,接下来m个正整数,
依次表示c[i][j](1<=c[i][j]<=1000000)。
 

 

Output

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

 

Sample Input

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

Sample Output

16

HINT

 

在第一家店买2号物品,在第二家店买剩下的物品。


 

 

Source

    状压DP。。。用f[i][j]表示前i个店买物品的状态是j的最小花费是多少。。然后转移就好了。。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[110][1<<16],n,m,c[110][20],d[110];
int read(){
	int x = 0,f = 1,c = getchar();
	while (c<'0' || c>'9') {if (c=='-') f = -1;c = getchar();}
	while (c>='0' && c<='9') x = x*10+c-'0',c = getchar();
	return x*f;
}
int main(){
	n = read();m = read();
	for (int i=1;i<=n;i++){
		d[i] = read();
		for (int j=1;j<=m;j++) c[i][j] = read();
	}
	memset(f,60,sizeof(f));
	f[0][0] = 0;
	for (int i=1;i<=n;i++){
		for (int j=0;j<(1<<m);j++)
			f[i][j] = f[i-1][j]+d[i];
		for (int k=1;k<=m;k++)
			for (int j=0;j<(1<<m);j++)
				if (!(j&(1<<(k-1))))
					f[i][j|(1<<(k-1))] = min(f[i][j|(1<<(k-1))],f[i][j]+c[i][k]);
		for (int j=0;j<(1<<m);j++) f[i][j] = min(f[i][j],f[i-1][j]);
	}
	printf("%d",f[n][(1<<m)-1]);
}
Category: BZOJ题解 | Tags: | Read Count: 354

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com