hdu4049之状态压缩dp

 
携程编程大赛(限2000人),你报名了吗?(4月7日报名截止)

Tourism Planning

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 983    Accepted Submission(s): 419


Problem Description
Several friends are planning to take tourism during the next holiday. They have selected some places to visit. They have decided which place to start their tourism and in which order to visit these places. However, anyone can leave halfway during the tourism and will never back to the tourism again if he or she is not interested in the following places. And anyone can choose not to attend the tourism if he or she is not interested in any of the places. 
Each place they visited will cost every person certain amount of money. And each person has a positive value for each place, representing his or her interest in this place. To make things more complicated, if two friends visited a place together, they will get a non negative bonus because they enjoyed each other’s companion. If more than two friends visited a place together, the total bonus will be the sum of each pair of friends’ bonuses.
Your task is to decide which people should take the tourism and when each of them should leave so that the sum of the interest plus the sum of the bonuses minus the total costs is the largest. If you can’t find a plan that have a result larger than 0, just tell them to STAY HOME.
 

Input
There are several cases. Each case starts with a line containing two numbers N and M ( 1<=N<=10, 1<=M<=10). N is the number of friends and M is the number of places. The next line will contain M integers Pi (1<=i<=M) , 1<=Pi<=1000, representing how much it costs for one person to visit the ith place. Then N line follows, and each line contains M integers Vij (1<=i<=N, 1<=j<=M), 1<=Vij<=1000, representing how much the ith person is interested in the jth place. Then N line follows, and each line contains N integers Bij (1<=i<=N, 1<=j<=N), 0<=Bij<=1000, Bij=0 if i=j, Bij=Bji.
A case starting with 0 0 indicates the end of input and you needn’t give an output.
 

Output
For each case, if you can arrange a plan lead to a positive result, output the result in one line, otherwise, output STAY HOME in one line.
 

Sample Input
       
       
       
       
2 1 10 15 5 0 5 5 0 3 2 30 50 24 48 40 70 35 20 0 4 1 4 0 5 1 5 0 2 2 100 100 50 50 50 50 0 20 20 0 0 0
 

Sample Output
       
       
       
       
5 41 STAY HOME
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=(1<<10)+10;
const int N=10+5;
int n,m;
int dp[MAX],temp[MAX],p[N],v[N][N],b[N][N],s[N];

int calw(int k,int c){
	int size=0,sum=0;
	for(int i=0;i<n;++i){
		if(k&(1<<i))s[++size]=i;
	}
	for(int i=1;i<=size;++i){
		sum+=v[s[i]][c];
		for(int j=i+1;j<=size;++j){
			sum+=b[s[i]][s[j]];
		}
	}
	return sum-size*p[c];
}

void DP(){
	int bit=1<<n,sum=-INF;
	for(int i=0;i<bit;++i)temp[i]=0;
	for(int i=1;i<=m;++i){
		for(int j=0;j<bit;++j)dp[j]=-INF;//这里必须是赋无穷而不能是-1 
		for(int j=0;j<bit;++j){
			int w=calw(j,i);
			for(int k=j;k<bit;k=(k+1)|j){
				dp[j]=max(dp[j],temp[k]+w);
			}
		}
		for(int j=0;j<bit;++j)temp[j]=dp[j];//采用滚动数组的思想节省内存,当然也可以开数组dp[i][2] 
	}
	for(int i=0;i<bit;++i)sum=max(sum,temp[i]);
	if(sum<=0)printf("STAY HOME\n");
	else printf("%d\n",sum);
}

int main(){
	while(~scanf("%d%d",&n,&m),n+m){
		for(int i=1;i<=m;++i)scanf("%d",p+i);
		for(int i=0;i<n;++i){
			for(int j=1;j<=m;++j)scanf("%d",&v[i][j]);
		}
		for(int i=0;i<n;++i){
			for(int j=0;j<n;++j){
				scanf("%d",&b[i][j]);
			}
		}
		DP();
	}
	return 0;
}
/*
1 2
1000 1
0 1000
0
STAY HOME
*/



你可能感兴趣的:(hdu4049之状态压缩dp)