HDU 4049 Tourism Planning(状压DP)题解
题意:m个城市,n个人,让这n个人按固定顺序走遍m个城市。每个城市有一个单人票价pi。每个人在每个城市能获得vij的价值。如果多个人在同一城市,那么会额外获得价值,给出一张n * n价值表,额外价值为任意两个组成队伍的价值和。每个人可以在中途退出,但是退出后不能再回来。问终点后最大价值。
思路:dp[st][i]表示在i城市状态st的最大价值,然后打表打出st的所有子集,每次都往子集转移。
代码:
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 10 + 5;
const int M = maxn * 30;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e4 + 7;
int dp[1 << maxn][maxn];
int p[maxn], v[maxn][maxn], b[maxn][maxn];
int eachother[1 << maxn], one[1 << maxn];
int nex[1 << 10][1 << 10 + 5], cnt[1 << 10];
int n, m;
int main(){
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < (1 << 10); i++){
for(int j = 0; j <= i; j++){
if((i & j) == j)
nex[i][cnt[i]++] = j;
}
} while(~scanf("%d%d", &n, &m) && n + m){
for(int i = 0; i < m; i++) scanf("%d", &p[i]);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
scanf("%d", &v[i][j]);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
scanf("%d", &b[i][j]); memset(eachother, 0, sizeof(eachother));
for(int i = 0; i < (1 << n); i++){
for(int j = 0; j < n; j++){
if(!((1 << j) & i)) continue;
for(int k = j + 1; k < n; k++){
if(!((1 << k) & i)) continue;
eachother[i] += b[j][k];
}
}
}
memset(dp, -INF, sizeof(dp));
for(int i = 0; i < (1 << n); i++){
dp[i][0] = 0;
for(int j = 0; j < n; j++){
if((1 << j) & i) dp[i][0] += v[j][0] - p[0];
}
dp[i][0] += eachother[i];
}
for(int i = 0; i < m - 1; i++){
for(int j = 0; j < (1 << n); j++){
for(int k = 0; k < cnt[j]; k++){
int st = nex[j][k];
int now = dp[j][i];
for(int f = 0; f < n; f++){
if((1 << f) & st) now += v[f][i + 1] - p[i + 1];
}
now += eachother[st];
dp[st][i + 1] = max(dp[st][i + 1], now);
}
}
} int ans = 0;
for(int i = 0; i < (1 << n); i++){
if(ans < dp[i][m - 1]){
ans = dp[i][m - 1];
}
}
if(ans > 0) printf("%d\n", ans);
else printf("STAY HOME\n");
}
return 0;
}
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/Oth ...
- 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 4272 LianLianKan (状压DP+DFS)题解
思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...
- HDU 4272 LianLianKan(状压DP)题解
题意:一个栈,每次可以选择和栈顶一样的数字,并且和栈顶距离小于6,然后同时消去他们,问能不能把所有的数消去 思路:一个数字最远能消去和他相距9的数,因为中间4个可以被他上面的消去.因为还要判断栈顶有没 ...
- HDU 4628 Pieces(状压DP)题解
题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #includ ...
- 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 ...
随机推荐
- IDEA安装codota插件和使用,开发人员的知心伙伴
打开IDEA 点击左上角的File之后,如下图 成功后如图所示
- 网络流量预测入门(一)之RNN 介绍
目录 网络流量预测入门(一)之RNN 介绍 RNN简介 RNN 结构 RNN原理 结构原理 损失函数$E$ 反向传播 总结 参考 网络流量预测入门(一)之RNN 介绍 了解RNN之前,神经网络的知识是 ...
- jdk安装简洁版
一.jdk是what? jdk其实是个软件语言开发包,包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具. 没有JDK的话,无法编译Java程序(指java源码.java文件),如果想 ...
- In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego Ongaro and John Ousterhout.
In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego ...
- 知乎社区核心业务 Golang 化实践 - 知乎 https://zhuanlan.zhihu.com/p/48039838
知乎社区核心业务 Golang 化实践 - 知乎 https://zhuanlan.zhihu.com/p/48039838
- (Oracle)常用的数据库函数
Trim: Trim() 函数的功能是去掉首尾空格. Eg: trim(to_char(level, '00')) Trunc: 1.TRUNC函数为指定元素而截去的日期值. trun ...
- 题解 UVA11694 【Gokigen Naname谜题 Gokigen Naname】
题目 题解 考场上连暴力都不会打的码农题,深搜是真的难 /kk 前置问题 怎么输出"\" cout<<"\\"; 2.怎么处理不在一个环里,可以考虑 ...
- 基于Koa2框架的项目搭建及实战开发
Koa是基于 Node.js 平台的下一代 web 开发框架,由express原班人马打造,致力于成为一个更小.更富有表现力.更健壮的 Web 框架.使用 koa 编写 web 应用,通过组合不同的 ...
- socket套接字编程(1)——基本函数
TCP交互流程: 服务器:1. 创建socket:2. 绑定socket和端口号:3. 监听端口号:4. 接收来自客户端的连接请求:5. 从socket中读取字符:6. 关闭socket. 客户端:1 ...
- 烧录失败导致boot无法加载的解决措施,再也不怕烧成砖了
目录: 1.usb烧录时出现的问题截图 2.重新擦除boot发现失败的情况 3.解决措施 烧录失败导致boot无法加载的解决措施在烧录系统的时候经常会遇到烧录失败的情况,如果能通过再次执行烧录能烧上肯 ...