HDU 4049 Tourism Planning (状压dp 详解)


Tourism Planning

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


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
 

Source
The 36th ACM/ICPC Asia Regional Beijing Site —— Online Contest

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4049

题目大意:有n人,m个景点,每个景点有一个花费,每个人对每个景点有一个喜爱值,若去某个景点则每个人的bonus为对该景点的喜爱值减去该景点的花费,若两个人同时到某个景点则总bonus加上一个额外值,两两到同一点的额外值通过一个n*n的矩阵表示,每个人可以在中途离开,一旦离开不得再回来,现在旅行路线已经确定,求怎样计划每个人的去留使得总的bonus最大,输出最大bonus,若最大bonus小于等于0,则输出STAY HOME

题目分析:状态压缩dp,dp[i][s]表示经过前i个城市时,状态为s的总bonus的值,状态s表示n个人在或不在的状态,比如dp[3][9]表示经过前三个城市时第一和第四个人还在的状态(9 == 1001),c[i][s]表示经过第i个城市,状态为s的bonus值,先预处理出来c数组,状态转移相当于是通过人数转移(必然是从人多的状态转移到人少的状态,因为离开就不能再回来)所以这里有两种写法(第二种比较快),详细见程序注释



#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
int n, m;
int dp[11][(1 << 11)], c[11][(1 << 11)];
int v[11][11], b[11][11], p[11];

void cal()
{
    for(int i = 1; i <= m; i++) //景点
    {
        for(int s = 0; s < (1 << n); s++) //状态
        {
            c[i][s] = 0;    //初始化为0
            for(int j = 0; j < n; j++)
            {   
                //经过前i个景点第j个人在的状态
                if(s & (1 << j))    
                {
                    //值=喜爱程度-花费
                    c[i][s] += v[j][i] - p[i];
                    //若在第j个人在的状态下,其前的第k个人也在
                    //则说明他们必然同时去了前i个景点,加上额外值
                    for(int k = 0; k < j; k++)
                        if(s & (1 << k))
                            c[i][s] += b[k][j];
                }
            }
        }
    }
}

int main()
{
    while(scanf("%d %d", &n, &m) != EOF && (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]);  //同一个地点多个人去的喜爱度额外值
        cal();  //计算每个城市每种状态下的值
        //初始化dp
        for(int i = 1; i <= m; i++)
            for(int s = 0; s < (1 << n); s++)
                dp[i][s] = -INF;
        int ans = -INF;
        // 写法1:
        // for(int i = 1; i <= m; i++) //景点
        //     for(int s = 0; s < (1 << n); s++) //当前状态s
        //         for(int j = (1 << n) - 1; j >= s; j--)  //s之后的所有状态,因为人走了就不能回来
        //             if((j & s) == s)    //若当前状态是之前某个状态的子状态
        //                 //相当于背包,经过前i个城市,状态为s的值可能由经过前i-1个城市状态为j
        //                 //(j-> s:有人离开)的值加上经过第i个城市状态为s的值
        //                 dp[i][s] = max(dp[i][s], dp[i - 1][j] + c[i][s]);

        // 写法2:
        for(int i = 1; i <= m; i++)
            for(int s = 0; s < (1 << n); s++)
            {
                for(int j = s; ; j = ((j - 1) & s)) 
                {
                    if(j == 0)  //向前枚举到0
                    {
                        dp[i][0] = max(dp[i][0], dp[i - 1][s] + c[i][0]);
                        break;
                    }
                    //枚举s的子状态(s -> j有人离开),与写法1正好相反 
                    dp[i][j] = max(dp[i][j], dp[i - 1][s] + c[i][j]);

                }
            }

        //求游览m个景点下的最大值
        for(int s = 0; s < (1 << n); s++)
            ans = max(ans, dp[m][s]);
        if(ans <= 0)
            printf("STAY HOME\n");
        else
            printf("%d\n", ans);
    }
}


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值