传送门

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 }

转载于:https://www.cnblogs.com/njczy2010/p/4324225.html

hdu 4049 Tourism Planning [ 状压dp ]相关推荐

  1. HDU 3001 三进制状压DP

    HDU 3001 三进制状压DP N个城市,M条道路,每条道路有其经过的代价,每一个城市最多能够到达两次,求走全然部城市最小代价,起点随意. 三进制状压.存储每一个状态下每一个城市经过的次数. 转移方 ...

  2. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  3. Tunnels HDU - 4856 (bfs状压dp)

    Tunnels HDU - 4856 Bob is travelling in Xi'an. He finds many secret tunnels beneath the city. In his ...

  4. Hdu 4856 Tunnels(状压dp)

    题目链接 Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. HDU - 4856 Tunnels(哈密顿路径+状压dp)

    题目链接:点击查看 题目大意:给出一个n*n的正方形网格,其中"."表示可以走的路,"#"表示障碍物,每次可以向上下左右任意方向走1格,花费1单位时间,再给出m ...

  6. HDU 4539郑厂长系列故事――排兵布阵(状压DP)

    HDU 4539  郑厂长系列故事――排兵布阵 基础的状压DP,首先记录先每一行可取的所哟状态(一行里互不冲突的大概160个状态), 直接套了一个4重循环居然没超时我就呵呵了 1 //#pragma ...

  7. HDU 5691 Sitting in Line 状压dp

    Sitting in Line 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5691 Description 度度熊是他同时代中最伟大的数学家,一切 ...

  8. hdu 6149 Valley Numer II(01背包套状压dp)

    题目链接:hdu 6149 Valley Numer II 题意: 给你N个点,有k个为高点,其他为低点,现在这N个点有m条边,问你最多能组成多少个两个高点一个低点,低点和两个高点都有边相连这样的状态 ...

  9. hdu 1074 状压dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题意:有n个作业,对于每一个作业有一个deadline,有一个完成这作业所需要的时间.如果超过d ...

最新文章

  1. Lambda中的常用sql方法
  2. Ka的递归编程练习 Part4|Hanoi汉诺塔,双色汉诺塔的也有
  3. 中南大学计算机辅助工艺设计,中南大学计算机辅助制造大作业.doc
  4. Django Form -- 对单个表单的组合验证
  5. linux 添加用户_linux ---添加普通用户账号
  6. mfc清除配置ini文件的数据_大数据环境搭建与配置——aptget源更新、CRT链接、文件服务器配置...
  7. java游戏运行环境_Java运行环境
  8. 学习jQuery之旅--新手必须知道的常用方法
  9. ajax调用webService
  10. 招募贴:Hadoop专业解决方案招募义务翻译人员
  11. python实验教程_python语言程序设计实践教程实验七
  12. 制作linux红帽光盘刻录,RedHat命令行刻录光盘
  13. pycharm IDEA专业版2016.3.2版本和 python3.5.0 win7 64位安装包 百度云资源共享 及安装和编辑器注册图录
  14. arduino小火车交通灯
  15. 如何为自己的 CSDN博客设置自定义域名?
  16. python怎么输出变量_Python每日小知识(1):输入和输出、数据类型和变量
  17. GAU-α:尝鲜体验快好省的下一代Attention
  18. 唐福林:新浪微博的Redis大数据之路
  19. (清华毕业生)大佬总结的“大数据”学习路线+教程
  20. VGA 接口电阻网络阻抗

热门文章

  1. 服务器tomcat配置教程
  2. 哪位大兄弟有用 cMake 开发Android ndk的
  3. HAProxy的日志配置以及ACL规则实现负载均衡
  4. 在二维数组中查找一个数
  5. phpRedisAdmin 安装
  6. 软件概要设计做什么,怎么做
  7. linux操作系统cp命令
  8. SpringMVC核心分发器DispatcherServlet分析[附带源码分析]
  9. linux操作命令等积累
  10. Plsql运行mysql脚本_oracle中PLSQL语句