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
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.
A case starting with 0 0 indicates the end of input and you needn’t give an output.
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
41
STAY HOME
| 13075053 | 2015-03-09 18:32:49 | Accepted | 4049 | 405MS | 2120K | 3076 B | G++ | czy |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#define N 15 using namespace std; int n,m;
int p[N];
int v[N][N];
int b[N][N];
int dp[N][ (<<) ];
int ans;
int tot;
int happy[N][ (<<) ]; vector<int> can[ (<<) ]; int cal(int i,int o);
int ok(int k,int o); void ini()
{
int i,j;
ans=;
memset(dp,,sizeof(dp));
for(i=;i<=m;i++){
scanf("%d",&p[i]);
}
for(i=;i<n;i++){
for(j=;j<=m;j++){
scanf("%d",&v[i][j]);
}
}
for(i=;i<n;i++){
for(j=;j<n;j++){
scanf("%d",&b[i][j]);
}
}
int o;
tot = (<<n);
for(i=;i<=m;i++){
for(o=;o<tot;o++){
dp[i][o]=-;
}
}
//printf(" n=%d m=%d tot=%d\n",n,m,tot );
for(i=;i<=m;i++){
for(o=;o<tot;o++){
happy[i][o]=cal(i,o);
}
} for(o=;o<tot;o++){
can[o].clear();
for(int k=;k<tot;k++){
if(ok(k,o)==){
can[o].push_back(k);
}
}
}
} int cal(int i,int o)
{
int re=;
int j,k;
int cc=;
//printf(" i=%d o=%d\n",i,o );
for(j=;j<n;j++){
if( (<<j) & o ){
cc++;
re+=v[j][i];
}
}
//printf(" 1 re=%d\n",re );
for(j=;j<n;j++){
if( (<<j) & o ){
for(k=j+;k<n;k++){
if( (<<k) & o ){
re += b[j][k];
}
}
}
}
// printf(" 2 re=%d\n",re );
re -= p[i]*cc;
//printf(" 3 re=%d\n",re );
//printf(" i=%d o=%d re=%d\n",i,o,re );
return re;
} int ok(int k,int o){
int j;
for(j=;j<n;j++){
// printf(" j=%d\n",j );
if( (<<j) & o ){
if( ( (<<j) &k ) == ){
return ;
}
}
}
return ;
} void solve()
{
int o,j,i,k;
int te;
for(i=;i<=m;i++){
//printf(" i=%d\n",i );
for(o=;o<tot;o++){
// printf(" o=%d\n", o);
for(vector<int>::iterator it =can[o].begin();it != can[o].end();it++){
// for(k=0;k<tot;k++){
//printf(" k=%d\n", k);
k=*it;
// if(ok(k,o)==0) continue; //te=cal(i,o);
te=happy[i][o];
dp[i][o]=max(dp[i][o],dp[i-][k]+te);
//printf(" i=%d o=%d dp=%d\n", i,o,dp[i][o]);
}
}
} i=m;
for(o=;o<tot;o++){
//printf(" o=%d dp=%d\n",o,dp[m][o] );
ans=max(ans,dp[m][o]);
}
} void out()
{
if(ans<=){
printf("STAY HOME\n");
}
else{
printf("%d\n", ans);
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
if(n== && m==) break;
ini();
solve();
out();
}
}
hdu 4049 Tourism Planning [ 状压dp ]的更多相关文章
- hdu 3247 AC自动+状压dp+bfs处理
Resource Archiver Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Ot ...
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 5765 Bonds(状压DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...
- hdu 3681(bfs+二分+状压dp判断)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...
- hdu 4778 Gems Fight! 状压dp
转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...
- hdu 4856 Tunnels (bfs + 状压dp)
题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...
- HDU 4272 LianLianKan (状压DP+DFS)题解
思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...
- HDU 3362 Fix (状压DP)
题意:题目给出n(n <= 18)个点的二维坐标,并说明某些点是被固定了的,其余则没固定,要求添加一些边,使得还没被固定的点变成固定的, 要求总长度最短. 析:由于这个 n 最大才是18,比较小 ...
- HDU 3001 Travelling (状压DP,3进制)
题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...
随机推荐
- poj1724 ROADS
题意: N个城市,编号1到N.城市间有R条单向道路.每条道路连接两个城市,有长度和过路费两个属性.Bob只有K块钱,他想从城市1走到城市N.问最短共需要走多长的路.如果到不了N,输出-12<=N ...
- 将vue-cli项目配置在nginx上
登录使用的是node.js搭建的注册登录接口,关于对数据库的读写则是用spring boot的框架来实现的. 1.首先是vue-cli项目里的前端页面的配置: location / { root ...
- chrome inspect出现白屏的解决方案
点inspect后 弹出框,可是里面一片白色 PS:原效果不是这样,只是图找不到随便p的 原因可以看这个:http://www.cnblogs.com/slmk/p/7591126.html 大概意思 ...
- 必看的dockerfile禁忌与建议!
直接上对照组(看第三个run) test1 FROM centos MAINTAINER ** RUN yum -y update RUN yum -y install wget RUN wg ...
- docker上配置nginx负载均衡
采用ubuntu系统,docker安装自行百度 1.安装tomcat docker run -d -p : tomcat docker run -d -p : tomcat 安装两个实例,端口分别为8 ...
- mysql 中modify和change区别(以及使用modify修改字段名称报错)
使用modify修改字段报错如下: mysql> alter table student modify name sname char(16);ERROR 1064 (42000): You h ...
- docker 新手入门 (阿里镜像仓库的使用)
创建镜像仓库后的步骤是: https://help.aliyun.com/document_detail/60743.html?spm=a2c4g.11186623.6.546.79be52f3y ...
- html自己写响应式布局(说起来很高大上的样子,但是其实很简单)
第一步,打开电脑中安装的Sublime Text3,新建demo文件夹用来存放文件,在里面新建一个HTML文件,通过Tab快捷键迅速创建一个HTML模板,并命名标题. 第二步,在Body标签里添加三个 ...
- zabbix告警邮件、短信发送错误快速排查方法
zabbix告警邮件.短信发送错误快速排查方法 背景 zabbix告警邮件.短信经常有同事反馈发送错误的情况,这个问题排查的角度很多,那么最快捷的角度是什么呢? 在我看来,最快的角度就是判断这个告警邮 ...
- centOS出现 -bash: vim: command not found
问题描述 用centos 的主机的時候, 用 vim 时出现 -bash: vim: command not found. 只能使用 vi. 那么如何安裝 vim 呢? 解决步骤 1.查看是否安装 输 ...