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.
A case starting with 0 0 indicates the end of input and you needn’t give an output.
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
5 41 STAY HOME
题目大概:
由n个游客去m个地方游玩,给出了去每个地方的花费,游客对每个地方的兴趣,两个地方都去还会有兴趣加成,这个n个游客可以在旅游的中途退出,但是退出后就不能回来了。问最大的兴趣值。
思路:
把这m个地方用二进制表示成0 1 不去或去。
一个人去一个地方的兴趣值=他的兴趣值+兴趣加成值 - 花费。
具体的在代码里。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int ma=12;
int dp[ma][1<<ma];
int cost[ma],in[ma][ma],bo[ma][ma];
int n,m;
int get(int x,int st)//x城市在st状态获得的兴趣
{
int ans=0;
for(int i=0;i<n;i++)//枚举n个城市
{
if(st&(1<<i))//是否已经去过
{
ans-=cost[x];//花费
ans+=in[i][x];//兴趣
for(int j=i+1;j<n;j++)//去多个地方的效应
{
if(st&(1<<j))
{
ans+=bo[i][j];
}
}
}
}
return ans;
}
int main()
{
while(~scanf("%d%d",&n,&m)&&n+m)
{
for(int i=0;i<m;i++)
{
scanf("%d",&cost[i]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&in[i][j]);
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&bo[i][j]);
}
}
for(int i=0;i<(1<<n);i++)//预处理
{
dp[0][i]=get(0,i);
}
for(int i=1;i<m;i++)//枚举每个城市
{
for(int j=0;j<(1<<n);j++)//每个城市都有每种状态
{
int ans=get(i,j);//在这个城市可获得的兴趣
dp[i][j]=dp[i-1][j]+ans;
for(int k=j+1;k<(1<<n);k++)//以前的状态可能人更多
{
if((k&j)==j)//以前的状态必须包含当前状态状态
{
dp[i][j]=max(dp[i][j],dp[i-1][k]+ans);
}
}
}
}
int ans=0;
for(int i=0;i<(1<<n);i++)
{
ans=max(ans,dp[m-1][i]);
}
if(ans)printf("%d\n",ans);
else printf("STAY HOME\n");
}
return 0;
}