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);
    }
}
时间: 2024-10-07 02:01:44

HDU 4049 Tourism Planning (状压dp 详解)的相关文章

hdu 4049 Tourism Planning [ 状压dp ]

传送门 Tourism Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1115    Accepted Submission(s): 482 Problem Description Several friends are planning to take tourism during the next holiday.

HDU 4924 Football Manager(状压DP)

题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=4924 题意 : n(<=20)个人选出11个人作为首发组成a-b-c阵容,每个人都有自己擅长的位置,并且每个人和其他人会有单向的厌恶和喜欢关系,每个人对于自己擅长的位置都有两个值CA和PA,有喜欢或者厌恶关系的两个人在一起也会影响整个首发的CA总值,要求选出一套阵容使得CA最大,或者CA一样的情况下PA最大. 思路 : 状压搞,dp[s]s的二进制表示20个人中选了那几个人,然后规定选进来的顺序

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2382    Accepted Submission(s): 750 Problem Description Great! Your new software is almost finished! The only thing left to

HDU 1074 Doing Homework(状压DP)

Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will r

HDU 5765 Bonds(状压DP)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不同的连通块,对于每条边,求出两边的联通块的划分方案数,就是对于该点的答案. [代码] #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int n,m,T,Cas=1

hdu 4778 Gems Fight! 状压dp

转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得到的最大值 代码也来自wdd 1 /****************************************************** 2 * File Name: b.cpp 3 * Author: kojimai 4 * Creater Time:2014年08月13日 星期三 11时

HDU 3001 Travelling (状压DP,3进制)

题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示,3进制可以先将表打出来.在走的时候注意只能走2次,其他的和普通的TSP状压DP是一样的.注意点:重边,自环等等,老梗了. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #i

HDU 4628 Pieces(状压DP)题解

题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include <iostream> #include<algorithm&