BZOJ 4145: [AMPPZ2014]The Prices

状压DP

暴力出奇迹

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int d[1000005],c[105][16],F[105][70005][2];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for (int i=1; i<=n; i++){
		scanf("%d",&d[i]);
		for (int j=0; j<m; j++) scanf("%d",&c[i][j]);
	}
	memset(F,40,sizeof(F));
	F[0][0][0]=0;
	for (int i=0; i<n; i++)
		for (int j=0; j<(1<<m); j++){
			F[i+1][j][0]=min(F[i+1][j][0],F[i][j][0]);
			F[i+1][j][0]=min(F[i+1][j][0],F[i][j][1]);
			for (int k=0; k<m; k++)
				if (!(j&(1<<k))){
					F[i][j|(1<<k)][1]=min(F[i][j|(1<<k)][1],F[i][j][0]+d[i+1]+c[i+1][k]);
					F[i][j|(1<<k)][1]=min(F[i][j|(1<<k)][1],F[i][j][1]+c[i+1][k]);
				}
		}
	printf("%d\n",min(F[n][(1<<m)-1][0],F[n][(1<<m)-1][1]));
	return 0;
}

  

posted @ 2018-10-29 19:31  ~Silent  阅读(117)  评论(0编辑  收藏  举报
Live2D