Tourism Planning


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1051    Accepted Submission(s): 460

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
  1. 2 1
  2. 10
  3. 15
  4. 5
  5. 0 5
  6. 5 0
  7. 3 2
  8. 30 50
  9. 24 48
  10. 40 70
  11. 35 20
  12. 0 4 1
  13. 4 0 5
  14. 1 5 0
  15. 2 2
  16. 100 100
  17. 50 50
  18. 50 50
  19. 0 20
  20. 20 0
  21. 0 0
 
Sample Output
  1. 5
  2. 41
  3. STAY HOME
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  4050 4044 4042 4048 4047 
 

题目大意:

输入描述:

第一行两个数字表示,有n个人,m个城市

接下来 m个数字表示每个人参观这些城市的花费

接下来n行m列表示每个人参观每个城市得到的满意度

接下来n行n列表示每参观一个城市互相之间的影响的额外满意度,Bij (1<=i<=N, 1<=j<=N), 0<=Bij<=1000, Bij=0 if i=j, Bij=Bji.

你可以安排这n个人中的任意多个依次参观这m个城市0~m-1,中途也可以让一个人退出,退出后不能再回来,问你最大的值?

值 = 每个人参观每个城市得到的满意度的和 + 互相之间的影响增加的满意度和 - 参观花费和 。

解题思路:

这题的核心是DP

(1)因为n<=10 ,m<=10 ,数据比较小,可以考虑比较暴力的做法,DP就是一个很好的暴力。

(2)很容易就想到这样的DP方程 DP[sum][k]=max{ DP[son][k+1] } + value[sum][k];

sum 就是用2进制表示的有哪些人,son就是sum的子状态,表示sum中一些人半途离开了还剩下的人,

k表示当前是在访问到哪个城市了,DP[sum][k]记录的是在这个状态下要求的最大值。

当有sum这些人访问到k这个城市时候,这时候中途退了一些人,转移到了son这些人,k+1城市 这个状态

转移的花费就是 sum这些人在k这个城市获得的总值,记为 value[sum][k]。

(3)唯一有点麻烦的就是value[sum][k]的数据预处理,这个暴力枚举。

解题代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. const int maxn=11;
  8. int n,m;//n people,m cities
  9. int cost[maxn],a[maxn][maxn],b[maxn][maxn];
  10. int dp[(1<<maxn)][maxn],vis[(1<<maxn)][maxn],val[(1<<maxn)][maxn],marked;
  11.  
  12. void input(){
  13. marked++;
  14. for(int i=0;i<m;i++) scanf("%d",&cost[i]);
  15. for(int i=0;i<n;i++){
  16. for(int j=0;j<m;j++){
  17. scanf("%d",&a[i][j]);
  18. }
  19. }
  20. for(int i=0;i<n;i++){
  21. for(int j=0;j<n;j++){
  22. scanf("%d",&b[i][j]);
  23. }
  24. }
  25. for(int i=0;i<m;i++){
  26. for(int sum=0;sum<(1<<n);sum++){
  27. val[sum][i]=0;
  28. for(int p1=0;p1<n;p1++){
  29. if( !((1<<p1)&sum) ) continue;
  30. for(int p2=0;p2<p1;p2++){
  31. if( (1<<p2)&sum ){
  32. val[sum][i]+=b[p1][p2];
  33. }
  34. }
  35. val[sum][i]+=a[p1][i]-cost[i];
  36. }
  37. }
  38. }
  39. }
  40.  
  41. int DP(int sum,int k){
  42. if(k>=m) return 0;
  43. if(vis[sum][k]==marked) return dp[sum][k];
  44. int ans=0;
  45. for(int x=sum;x!=0;x=(x-1)&sum ){//for every son state
  46. int tmp=DP(x,k+1)+val[sum][k];
  47. if(tmp>ans) ans=tmp;
  48. }
  49. vis[sum][k]=marked;
  50. return dp[sum][k]=ans;
  51. }
  52.  
  53. void solve(){
  54. int ans=0;
  55. for(int i=0;i<(1<<n);i++){
  56. if(DP(i,0)>ans) ans=DP(i,0);
  57. }
  58. if(ans==0) printf("STAY HOME\n");
  59. else printf("%d\n",ans);
  60. }
  61.  
  62. int main(){
  63. while(scanf("%d%d",&n,&m)!=EOF && (n||m) ){
  64. input();
  65. solve();
  66. }
  67. return 0;
  68. }

版权声明:欢迎关注我的博客,本文为博主toyking原创文章,未经博主允许不得转载。

HDU 4049 Tourism Planning(动态规划)的更多相关文章

  1. hdu 4049 Tourism Planning [ 状压dp ]

    传送门 Tourism Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. HDU 4049 Tourism Planning(状压DP)题解

    题意:m个城市,n个人,让这n个人按固定顺序走遍m个城市.每个城市有一个单人票价pi.每个人在每个城市能获得vij的价值.如果多个人在同一城市,那么会额外获得价值,给出一张n * n价值表,额外价值为 ...

  3. Tourism Planning(HDU 4049状压dp)

    题意:n个朋友去游览m个城市,给出每个人对每个城市的兴趣值,和每人去每个城市的花费,如果两人在一个城市有一个价值,若多于2人这这个价值是任意两人产生价值的总和.在去每个城市的过程中人可以随便离开,也可 ...

  4. hdu FatMouse's Speed 动态规划DP

    动态规划的解决方法是找到动态转移方程. 题目地址:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=3&sectionid ...

  5. HDU 2059 龟兔赛跑(动态规划)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. HDU 3634 City Planning (离散化)

    City Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. HDU 2103 Family planning

    http://acm.hdu.edu.cn/showproblem.php?pid=2103 Problem Description As far as we known,there are so m ...

  8. hdu 2059:龟兔赛跑(动态规划 DP)

    龟兔赛跑 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  9. hdu 3008:Warcraft(动态规划 背包)

    Warcraft Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

随机推荐

  1. 四、Handler(WSGIHandler)

    1.1       类视图关系 Handler主要负责处理HTTP请求,并生成相应的相应,process_request,process_response是两个最主要的成员.下图是WSGIHandle ...

  2. .net winform程序下使用firefox作为Web浏览器

    在winform程序中,要在程序中展示一个web页面,最常用的就是.net自带的webbrowser,但是大家都知道它是IE,也知道IE是有多么强(er)大(bi).而且微软已经宣布了IE的死亡... ...

  3. ServletFileUpload(Servlet文件上传)

    //**文件上传** form表单提交必须指定Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型"multipart/form-data" //1.创建磁盘文件项目工 ...

  4. C#多态--虚方法实现多态

    1.虚方法提供一种默认实现,子类可以选择是否重写,如果不重写,那么就使用父类已经实现的方法.(重写可以改变方法的指针) 如果需要改变类型指针,那么需要做方法的重写: 1.如果子类方法是重写方法,那么系 ...

  5. Unity3D入门基本概念整理

    1. (1)在场景中添加资源 只需单击工程视图 (Project View) 中的网格(Mesh)并拖动至层级视图 (Hierarchy) 或场景视图 (Scene View),便可将其添加至场景 ( ...

  6. C#调用webservice 时如何传递实体对象

    在webservice端公开一个实体类,然后实例化,赋值,然后再给到webservice,可以实现,但是,即使调用端和service端的实体类完全一致,你也要重新实例化service端的,重新赋值,将 ...

  7. JS获取html对象的几种方式说明

    document.getElementById("zx"); 通过ID获取html元素对象,ID号在html文档当中应该是唯一的.返回的是唯一element对象.并且所有浏览器都兼 ...

  8. 如何理解css中的float

    最近一段时间一直在为一个即将上线的新站进行一些前端开发.自然,对CSS的使用是必不可少的了.我们在CSS 中很多时候会用到浮动来布局.常见的有 float:left 或者 float:right .简 ...

  9. 404 Not Found错误页面的解决方法和注意事项

    最近这段时间一直忙于整理网站的错误页面,期间整理了很多关于404 Not Found错误页面的知识,加之最近也在帮团队新来的人员培训seo优化知识,所以在此借助马海祥博客的平台就拿出来跟大家一起分享一 ...

  10. [python拾遗]列表

    python列表拾遗 1.列表可以修改,使用 ‘+’ 将一个新列表附加在原列表的尾部: >>> a = [1,'a'] >>> b = a + [2,'b'] &g ...