题意: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)题解的更多相关文章

  1. hdu 4049 Tourism Planning [ 状压dp ]

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

  2. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  3. hdu 2825 aC自动机+状压dp

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

  4. HDU 4272 LianLianKan (状压DP+DFS)题解

    思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...

  5. HDU 4272 LianLianKan(状压DP)题解

    题意:一个栈,每次可以选择和栈顶一样的数字,并且和栈顶距离小于6,然后同时消去他们,问能不能把所有的数消去 思路:一个数字最远能消去和他相距9的数,因为中间4个可以被他上面的消去.因为还要判断栈顶有没 ...

  6. HDU 4628 Pieces(状压DP)题解

    题意:n个字母,每次可以删掉一组非连续回文,问你最少删几次 思路:把所有回文找出来,然后状压DP 代码: #include<set> #include<map> #includ ...

  7. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  8. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  9. hdu 4778 Gems Fight! 状压dp

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

  10. hdu 4856 Tunnels (bfs + 状压dp)

    题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...

随机推荐

  1. 庐山真面目之十一微服务架构手把手教你搭建基于Jenkins的企业级CI/CD环境

    庐山真面目之十一微服务架构手把手教你搭建基于Jenkins的企业级CI/CD环境 一.介绍 说起微服务架构来,有一个环节是少不了的,那就是CI/CD持续集成的环境.当然,搭建CI/CD环境的工具很多, ...

  2. Flask源码关于local的实现

    flask源码关于local的实现 try: # 协程 from greenlet import getcurrent as get_ident except ImportError: try: fr ...

  3. 解决JavaScript中构造函数浪费内存的问题!

    解决JavaScript中构造函数浪费内存的问题! 把构造函数中的公共的方法放到构造函数的原型对象上! // 构造函数的问题! function Gouzaohanshu(name, age, gen ...

  4. 两个报文是如何进行 TCP 分组传输

    16 | 如何理解TCP的"流"? https://time.geekbang.org/column/article/132443 TCP 是一种流式协议在前面的章节中,我们讲的都 ...

  5. 【rz】【sz】参数详解

    参数 SYNOPSIS sz [-+8abdefkLlNnopqTtuvyY] file ... b:以二进制方式,默认为文本方式 e:对所有控制字符转义 待续 常见问题: 1.xshell 使用rz ...

  6. P6584 重拳出击

    写在前面 来给 zrm 大佬的题写一篇题解. 这题代码实现难度不高,但是比较锻炼思维,而且应该有不少种解法.着实是一道质量很高的题目. 算法思路 首先呢,显然当小 Z 向当前节点的一棵子树走去时,这棵 ...

  7. Docker Desktop启动Kubernetes

    Docker_Desktop启动Kubernetes 参考仓库:https://github.com/AliyunContainerService/k8s-for-docker-desktop 视频参 ...

  8. Weblogic漏洞利用

    Weblogic漏洞 Weblogic任意文件上传(CVE-2018-2894) 受影响版本 weblogic 10.3.6.0.weblogic 12.1.3.0.weblogic 12.2.1.2 ...

  9. MySQL常见优化

    MySQL常见优化 1.操作符优化 1.1<> 操作符(不等于) 1.2LIKE优化 1.3in,not in,exists与not exists 1.3.1in和exists 2.whe ...

  10. Linux环境Hive安装配置及使用

    Linux环境Hive安装配置及使用 一.Hive Hive环境前提 二.Hive架构原理解析 三.Hive-1.2.2单机安装流程 (1) 解压apache-hive-1.2.2-bin.tar.g ...