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
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int L = 1100;
const int inf = 1<<30;
int dp[L][20],next[L][L],len[L],mem[L][20];
int cost[20],val[20][20],with[20][20];
int n,m,nt;
void solve()
{
int i,j,k;
nt = 1<<n;
for(i = 0; i<nt; i++)
{
len[i] = 0;
for(j = 0; j<nt; j++)//next[i][j] = k,表示由i变化的第k个状态为j
if((i&j) == j)
next[i][len[i]++] = j;
}
for(i = 0; i<nt; i++)
{
int tem = 0,cnt = 0;
for(j = 0; j<n; j++)
{
if((1<<j) & i)//J在状态之中
{
cnt++;
for(k = 0; k<j; k++)//k也在状态之中,那么为一组
if((1<<k) & i)
tem+=with[j][k];
}
}
for(j = 0; j<m; j++)//枚举城市
{
mem[i][j] = tem;
mem[i][j] -=cnt*cost[j];//减去花费
for(k = 0; k<n; k++)
{
if((1<<k) & i)//这个人存在与状态中,加上高兴值
mem[i][j]+=val[k][j];
}
}
}
for(i = 0; i<nt; i++)
{
for(j = 0; j<m; j++)
dp[i][j] = -inf;
dp[i][0] = mem[i][0];
}
for(i = 1; i<m; i++)//dp出各个状态的值
for(j = 0; j<nt; j++)
for(k = 0; k<len[j]; k++)
dp[next[j][k]][i] = max(dp[next[j][k]][i],dp[j][i-1]+mem[next[j][k]][i]);
}
int main()
{
int i,j,k;
while(~scanf("%d%d",&n,&m),n+m)
{
for(i = 0; i<m; i++)
cin>>cost[i];
for(i = 0; i<n; i++)
for(j = 0; j<m; j++)
cin>>val[i][j];
for(i = 0; i<n; i++)
for(j = 0; j<n; j++)
cin>>with[i][j];
solve();
int ans = -inf;
for(i = 0; i<nt; i++)
ans = max(ans,dp[i][m-1]);
if(ans<=0)
printf("STAY HOME\n");
else
printf("%d\n",ans);
}
return 0;
}