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进制来表示 ...
随机推荐
- Web前端开发的四个阶段(小白必看)
第一阶段:HTML的学习 超文本标记语言(HyperText Mark-up Language 简称HTML)是一个网页的骨架,无论是静态网页还是动态网页,最终返回到浏览器端的都是HTML代码,浏览器 ...
- js dom node.children与node.childNodes区别
不同点:node.children不会取到节点下面的TextNode但是node.childNodes会取到 共同点:两者都是集合类数组,可以通过索引的方式取到值也可以用for循环遍历
- iOS 解决iOS 9下的http请求发送失败问题
iOS9中 因为系统要求所有的请求都必须使用https, 所以发送http请求会失败,如果想让程序能够兼容http请求 在info.plist中添加以下代码: 这里需要做的是右键info.plist文 ...
- win10忘记wifi记录
1.点击桌面右下角无线图标 2.点击网络设置 3.点击管理WIFI设置 4.点击要管理的账户,忘记或者共享该wifi.
- Using URL Schemes to Communicate with Apps
要素:1)通信双方:2)协议:3)支持机制(系统,进行协议注册):4)数据 https://developer.apple.com/library/content/documentation/iPho ...
- socket是什么?协议栈操作的抽象
http://www.cnblogs.com/airtcp/p/5230161.html TCP/IP只是一个协议栈,就像操作系统的运行机制一样,必须要具体实现,同时还要提供对外的操作接口.就像操作系 ...
- CodeForces - 1059D——二分/三分
题目 题目链接 简单的说,就是作一个圆包含所有的点且与x轴相切,求圆的最小半径 方法一 分析:求最小,对半径而言肯定满足单调性,很容易想到二分.我们二分半径,然后由于固定了与X轴相切,我们对于每一个点 ...
- QT_7_资源文件_对话框_QMessageBox_界面布局_常用控件
资源文件 1.1. 将资源导入到项目下 1.2. 添加文件—>Qt -->Qt Resource File 1.3. 起名称 res ,生成res.qrc文件 1.4. 右键 open i ...
- css控制超出部分自动省略...
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 常用的网络通信命令--write.wall.mesg.mail
write 作用:给其它的在线用户发送消息 格式:write [ 用户名 ] [ tty ] 注意点:使用之前最好使用 who 命令查看当前在线用户,tty 为端口号 使用举例: 在光标闪烁的地方 ...