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.
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.
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
Source
Recommend
lcy
这题比赛的时候剩下半个小时,果断敲最后一题,结果哪题都没出来,其实这题还是比较好想的,只是预处理了很多东西写的比较复杂吧。
直接贴代码了
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define INF 999999999
int cost[15];
int interest[15][15];
int pairr[15][15];
vector <int> av[(1<<10)+2];
int add[(1<<10)+2];
int cnt[(1<<10)+2];
int dp[15][(1<<10)+2];
int local[15][(1<<10)+2];
int main()
{
int i,j,n,m,k,ans,t,kk;
for (i=0;i<(1<<10);i++)
{
for (j=0;j<=i;j++)
{
for (k=0;k<10;k++)
{
if ((i & (1<<k))==0 && (j & (1<<k))!=0) break;
}
if (k==10)
{
av[i].push_back(j);
}
}
}
memset(cnt,0,sizeof(cnt));
for (i=0;i<(1<<10);i++)
{
k=i;
while(k)
{
cnt[i]+=(k&1);
k>>=1;
}
}
while(scanf("%d%d",&n,&m)!=EOF)
{
if (n==0 && m==0) break;
for (i=0;i<m;i++)
{
scanf("%d",&cost[i]);
}
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
scanf("%d",&interest[i][j]);
}
}
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&pairr[i][j]);
}
}
memset(add,0,sizeof(add));
for (i=0;i<(1<<n);i++)
{
for (j=0;j<n;j++)
{
if ((i & (1<<j))==0) continue;
for (k=j+1;k<n;k++)
{
if ((i & (1<<j))!=0 && (i & (1<<k))!=0)
{
add[i]+=pairr[j][k];
}
}
}
}
for (i=0;i<m;i++)
{
for (j=0;j<(1<<n);j++)
{
dp[i][j]=-INF;
}
}
memset(local,0,sizeof(local));
for (i=0;i<m;i++)
{
for (j=0;j<(1<<n);j++)
{
for (k=0;k<n;k++)
{
if ((j & (1<<k))!=0) local[i][j]+=interest[k][i];
}
}
}
for (i=0;i<(1<<n);i++)
{
dp[0][i]=add[i]+local[0][i]-cost[0]*cnt[i];
}
for (i=0;i<m-1;i++)
{
for (j=0;j<(1<<n);j++)
{
t=av[j].size();
for (k=0;k<t;k++)
{
kk=av[j][k];
dp[i+1][kk]=max(dp[i+1][kk],dp[i][j]+local[i+1][kk]+add[kk]-cost[i+1]*cnt[kk]);
}
}
}
ans=0;
for (i=0;i<(1<<n);i++)
{
ans=max(ans,dp[m-1][i]);
}
if (ans==0) printf("STAY HOME\n");
else printf("%d\n",ans);
}
return 0;
}