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

Recommend

lcy   |   We have carefully selected several similar problems for you:  4041 4043 4050 4044 4045

13075053 2015-03-09 18:32:49 Accepted 4049 405MS 2120K 3076 B G++ czy
  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <string>
  7 #define N 15
  8
  9 using namespace std;
 10
 11 int n,m;
 12 int p[N];
 13 int v[N][N];
 14 int b[N][N];
 15 int dp[N][ (1<<10) ];
 16 int ans;
 17 int tot;
 18 int happy[N][ (1<<10) ];
 19
 20 vector<int> can[ (1<<10) ];
 21
 22 int cal(int i,int o);
 23 int ok(int k,int o);
 24
 25 void ini()
 26 {
 27     int i,j;
 28     ans=0;
 29     memset(dp,0,sizeof(dp));
 30     for(i=1;i<=m;i++){
 31         scanf("%d",&p[i]);
 32     }
 33     for(i=0;i<n;i++){
 34         for(j=1;j<=m;j++){
 35             scanf("%d",&v[i][j]);
 36         }
 37     }
 38     for(i=0;i<n;i++){
 39         for(j=0;j<n;j++){
 40             scanf("%d",&b[i][j]);
 41         }
 42     }
 43     int o;
 44     tot = (1<<n);
 45     for(i=1;i<=m;i++){
 46         for(o=0;o<tot;o++){
 47             dp[i][o]=-1000000000;
 48         }
 49     }
 50     //printf("  n=%d m=%d tot=%d\n",n,m,tot );
 51     for(i=1;i<=m;i++){
 52         for(o=0;o<tot;o++){
 53             happy[i][o]=cal(i,o);
 54         }
 55     }
 56
 57     for(o=0;o<tot;o++){
 58         can[o].clear();
 59         for(int k=0;k<tot;k++){
 60             if(ok(k,o)==1){
 61                 can[o].push_back(k);
 62             }
 63         }
 64     }
 65 }
 66
 67 int cal(int i,int o)
 68 {
 69     int re=0;
 70     int j,k;
 71     int cc=0;
 72     //printf("  i=%d o=%d\n",i,o );
 73     for(j=0;j<n;j++){
 74         if( (1<<j) & o ){
 75             cc++;
 76             re+=v[j][i];
 77         }
 78     }
 79     //printf(" 1   re=%d\n",re );
 80     for(j=0;j<n;j++){
 81         if( (1<<j) & o ){
 82             for(k=j+1;k<n;k++){
 83                 if( (1<<k) & o ){
 84                     re += b[j][k];
 85                 }
 86             }
 87         }
 88     }
 89     // printf("  2  re=%d\n",re );
 90     re -= p[i]*cc;
 91      //printf("   3 re=%d\n",re );
 92     //printf("  i=%d o=%d re=%d\n",i,o,re );
 93     return re;
 94 }
 95
 96 int ok(int k,int o){
 97     int j;
 98     for(j=0;j<n;j++){
 99        // printf("    j=%d\n",j );
100         if( (1<<j) & o ){
101             if(  (  (1<<j) &k ) ==0 ){
102                 return 0;
103             }
104         }
105     }
106     return 1;
107 }
108
109 void solve()
110 {
111     int o,j,i,k;
112     int te;
113     for(i=1;i<=m;i++){
114         //printf("  i=%d\n",i );
115         for(o=0;o<tot;o++){
116            // printf("   o=%d\n", o);
117             for(vector<int>::iterator it =can[o].begin();it != can[o].end();it++){
118            // for(k=0;k<tot;k++){
119                  //printf("   k=%d\n", k);
120                 k=*it;
121                // if(ok(k,o)==0) continue;
122
123                 //te=cal(i,o);
124                 te=happy[i][o];
125                 dp[i][o]=max(dp[i][o],dp[i-1][k]+te);
126                 //printf("    i=%d o=%d dp=%d\n", i,o,dp[i][o]);
127             }
128         }
129     }
130
131     i=m;
132     for(o=0;o<tot;o++){
133         //printf("  o=%d dp=%d\n",o,dp[m][o] );
134         ans=max(ans,dp[m][o]);
135     }
136 }
137
138 void out()
139 {
140     if(ans<=0){
141         printf("STAY HOME\n");
142     }
143     else{
144         printf("%d\n", ans);
145     }
146 }
147
148 int main()
149 {
150     while(scanf("%d%d",&n,&m)!=EOF){
151         if(n==0 && m==0) break;
152         ini();
153         solve();
154         out();
155     }
156 }
时间: 2024-11-09 15:28:10

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): 1125    Accepted Submission(s): 487 Problem Description Several friends are planning to take tourism during the next holiday. Th

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&