您的位置:首页 > 其它

K - Tourism Planning (状态dp)

2017-12-07 17:42 155 查看
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. 

InputThere 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.

OutputFor 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


题意:一些朋友结伴旅游,他们的路线及起点终点都已经确定。每一个人都有权中途离开队伍,或者干脆第一座城市就不去,但是一旦离开就不可以再回来。每座城市都有一个消费值cost,所有人在该城市的消费一样。第 i 个人对第 j 座城市有一个兴趣值inter[i][j]。只要两个人呆在一起,他们就可以获得一个共享福利 bonus[i][j].若有许多人呆在一起,那么总福利是两两之和。现在考虑 cost, inter, bonus, 要求可能获得的总最大值。若总最大值为负,则输出 STAY HOME

思路:

状态dp

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

int n,m;
int cost[11],value[11][11],other[11][11];
int c[11][1<<11],dp[11][1<<11];

void solve()
{
int i,j,k,s;
for(i=1;i<=m;i++)//枚举每一个景点
{
for(s=0;s<(1<<n);s++)//每一个景点的每一种状态
{
c[i][s]=0;
for(j=0;j<n;j++)//每一个人是否留下
{
if(s&(1<<j))
{
c[i][s]+=value[j][i]-cost[i];
for(k=0;k<j;k++)//另外一个人要不要一起去,前面已经判断了这k个人
{
if((s&(1<<k)))
{
//c[i][s]+=value[k][i]-cost[i];
c[i][s]+=other[j][k];
}
}
}
}
}
}
}

int main()
{
int i,j,s;
while(~scanf("%d%d",&n,&m))
{
if(n+m==0) break;
for(i=1;i<=m;i++)
scanf("%d",&cost[i]);
for(i=0;i<n;i++)
for(j=1;j<=m;j++)
scanf("%d",&value[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&other[i][j]);
solve();
for(i=1;i<=m;i++)
for(s=0;s<(1<<n);s++)
dp[i][s]=-0x3f3f3f3f;
int ans=-0x3f3f3f3f;
for(i=1;i<=m;i++)//每一个景点
for(s=0;s<(1<<n);s++)
for(j=(1<<n)-1;j>=s;j--)
if((j&s)==s)//j状态完全包含s状态
dp[i][s]=max(dp[i][s],dp[i-1][j]+c[i][s]);//一定从人多转移到人少
for(s=0;s<(1<<n);s++)
ans=max(ans,dp[m][s]);
if(ans<=0)
printf("STAY HOME\n");
else
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM 状态dp 算法