HDU 3377 Plan (插头DP,变形)
题意:有一个n*m的矩阵,每个格子中有一个值(可能负值),要从左上角走到右下角,求路径的最大花费。
思路:
除了起点和终点外,其他的点可以走,也可以不走。
(2)我用的是括号表示法,所以起始状态为')',即仅有一个右括号,那么到右下角也应该是只有一个右括号。因为,如果碰到()),加粗表示起点的那个右括号,那么合并后变成)##,仍然是右括号,如果是)(),那么合并后变成##),仍然是右括号,相当于延续了。插头每到达一个格子就先将其值给加上,如果要合并的时候,再减掉(因为多算了一次),因此,新括号的出现,就需要多加上3个格子的值了。
(2)还有一个更直观的办法,就是添加半个圈,从起点到终点,设他们都为必走的格子,那就跟FZU 1977 PANDORA ADVENTURE (插头DP,常规)差不多了,只是没有了障碍格子而已。
比如矩阵:
000
000
000
可以建图为(其中b为必走的格子):
bbbb
b00b
000b
00bb
由于不用这样建图也是很容易解决的,所以下面代码就不这样建图了。
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int N=;
int g[N][N], cur, n, m;
struct Hash_Map
{
static const int mod=;
static const int NN=;
int head[mod]; //桶指针
int next[NN]; //记录链的信息
LL status[NN]; //状态
LL value[NN]; //状态对应的DP值。
int size; void clear() //清除哈希表中的状态
{
memset(head, -, sizeof(head));
size = ;
} void insert(LL st, LL val) //插入状态st的值为val
{
int h = st%mod;
for(int i=head[h]; i!=-; i=next[i])
{
if(status[i] == st) //这个状态已经存在,累加进去。
{
value[i] = max(value[i], val);
return ;
}
} status[size]= st; //找不到状态st,则插入st。
value[size] = val;
next[size] = head[h] ; //新插入的元素在队头
head[h] = size++;
}
}hashmap[]; inline int getbit(LL s,int pos) //取出状态s的第pos个插头
{
return (s>>*pos)&;
}
inline int setbit(LL s,int pos,int bit) //将状态s的第pos个插头设置为bit
{
if(s&(<<*pos )) s^=<<(*pos);
if(s&(<<(*pos+))) s^=<<(*pos+);
return (s|(bit<<*pos));
} int Fr(LL s,int pos,int bit) //寻找状态s的第pos个插头对应的右括号。
{
int cnt=;
for(pos+=; pos<m; pos++)
{
if(getbit(s, pos)==-bit) cnt++;
if(getbit(s, pos)==bit) cnt--;
if(cnt==-) return setbit(s, pos, -bit);
}
}
int Fl(LL s,int pos,int bit) //寻找状态s的第pos个插头对应的左括号。
{
int cnt=;
for(pos--; pos>=; pos--)
{
if(getbit(s, pos)==-bit) cnt++;
if(getbit(s, pos)==bit) cnt--;
if( cnt==-) return setbit(s, pos, -bit);
}
}
LL ans;
void DP(int i,int j)
{
for(int k=; k<hashmap[cur^].size; k++)
{
LL s=hashmap[cur^].status[k];
LL v=hashmap[cur^].value[k];
int R=getbit(s, j), D=getbit(s, j+);
LL t=(setbit(s,j,)&setbit(s,j+,));
if(R && D) //两个括号
{ if(R==D) //同个方向的括号
{
if(R==) t=Fr(t, j, ); //要改
else t=Fl(t, j, );
hashmap[cur].insert(t, v-g[i][j]);
}
else if( R== && D== ) //不同的连通分量
hashmap[cur].insert(t, v-g[i][j]);
}
else if(R || D) //仅1个括号
{
if( i+==n && j+==m && t== && ( R== || D== ) ) //终点才能闭合
ans=max(ans, v);
if(R) //右插头
{
if(i+<n ) hashmap[cur].insert(s, v+g[i+][j]);//往下
if(j+<m ) hashmap[cur].insert(setbit(t,j+,R), v+g[i][j+]);//往右
}
else //下插头
{
if(j+<m ) hashmap[cur].insert(s, v+g[i][j+]); //往右
if(i+<n ) hashmap[cur].insert(setbit(t,j,D), v+g[i+][j]);//往下
}
}
else
{
if( i+j ) hashmap[cur].insert(s, v); //不装括号
if( j+<m && i+<n && i+j ) //新括号
hashmap[cur].insert( setbit(s,j,)|setbit(s,j+,), v+g[i][j]+g[i+][j]+g[i][j+]);
}
}
} void cal()
{
for(int i=; i<n; i++)
{
cur^=;
hashmap[cur].clear();
for(int j=; j<hashmap[cur^].size; j++) //新行,需要左移一下状态。
hashmap[cur].insert( hashmap[cur^].status[j]<<, hashmap[cur^].value[j] );
for(int j=; j<m; j++)
{
cur^=;
hashmap[cur].clear();
DP(i,j);
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
int Case=;
while(~scanf("%d%d",&n,&m))
{
memset(g, , sizeof(g));
ans=-INF; //注意
cur=;
for(int i=; i<n; i++) //输入
for(int j=; j<m; j++)
scanf("%d",&g[i][j]); hashmap[cur].clear();
hashmap[cur].insert(, g[][]);
cal();
printf("Case %d: %lld\n", ++Case, ans);
}
return ;
}
AC代码
HDU 3377 Plan (插头DP,变形)的更多相关文章
- HDU 3377 Plan
Problem Description One day, Resty comes to an incredible world to seek Eve -- The origin of life. L ...
- HDU 4285 circuits( 插头dp , k回路 )
circuits Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 插头DP专题
建议入门的人先看cd琦的<基于连通性状态压缩的动态规划问题>.事半功倍. 插头DP其实是比较久以前听说的一个东西,当初是水了几道水题,最近打算温习一下,顺便看下能否入门之类. 插头DP建议 ...
- 插头dp练习
最近学了插头dp,准备陆续更新插头dp类练习. 学习论文还是cdq那篇<基于连通性状态压缩的动态规划问题>. 基本的想法都讲得很通透了,接下来就靠自己yy了. 还有感谢kuangbin大大 ...
- HDU 4113 Construct the Great Wall(插头dp)
好久没做插头dp的样子,一开始以为这题是插头,状压,插头,状压,插头,状压,插头,状压,无限对又错. 昨天看到的这题. 百度之后发现没有人发题解,hust也没,hdu也没discuss...在acm- ...
- HDU 4064 Carcassonne(插头DP)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4064 Problem Description Carcassonne is a tile-based ...
- hdu 1693 Eat the Trees——插头DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...
- HDU 4949 Light(插头dp、位运算)
比赛的时候没看题,赛后看题觉得比赛看到应该可以敲的,敲了之后发现还真就会卡题.. 因为写完之后,无限TLE... 直到后来用位运算代替了我插头dp常用的decode.encode.shift三个函数以 ...
- HDU 1693 Eat the Trees(插头DP、棋盘哈密顿回路数)+ URAL 1519 Formula 1(插头DP、棋盘哈密顿单回路数)
插头DP基础题的样子...输入N,M<=11,以及N*M的01矩阵,0(1)表示有(无)障碍物.输出哈密顿回路(可以多回路)方案数... 看了个ppt,画了下图...感觉还是挺有效的... 参考 ...
随机推荐
- web面试常见问题
1事件继承 function ClassA(sColor) { this.color = sColor; this.sayColor = function () { al ...
- Linux 执行定时任务 shell脚本
Linux上面执行定时任务,我们可以利用crontab -e直接编辑定时任务 另外我们还可以写好shell脚本,定时去执行shell脚本,这两个方法都可以起到定时执行的作用 下面我详细说一下入如何执行 ...
- Flutter实战视频-移动电商-44.详细页_首屏自定义Widget编写
44.详细页_首屏自定义Widget编写 把详细页的图片.标题.编号和价格形成一个单独的widget去引用 详情页的顶部单独封装个插件 在pages下面新建detials_page的文件件并在里面新建 ...
- Linux 获取文件属性
使用stat/lstat获取文件属性 头文件:#include <sys/types.h> #include <sys/stat.h> int stat(const char ...
- Django框架的安装,项目创建
目录 Django框架的安装,项目创建 方法安装 Django版本选择 1.11.21(最新LTS版) django管理目录 命令行创建项目 django项目 命令行启动 (必须在项目文件下执行) p ...
- hyperledger fabric 1.0.5 分布式部署 (七)
fabric 使用 fabric-ca 服务 准备部分 首先需要用户从github上download fabric-ca 的工程代码 cd $GOPATH/src/github.com/hyperle ...
- suse 11入门学习
1 去掉显示隐藏文件的默认配置 vi /etc/bash.bashrc 找到 ; then LS_OPTIONS="-A -N $LS_OPTIONS -T 0" else LS_ ...
- css-float浮动详细
前言 pc端的页面为了保持良好的兼容性,一般会使用css2部分就支持的浮动(float)和定位(postion)来布局.浮动行为怪异,但有迹可循.以下下是在css揭秘一书中总结的浮动内幕. 包含块:浮 ...
- 使用MethodSwizzle导致按home app进入后台或者app间切换发生crash的解决方法
参考文章: 1.http://blog.csdn.net/alincexiaohao/article/details/45913857 2.http://www.cocoachina.com/ios/ ...
- VLAN-6-VLAN Trunk协议(VTP)
VTP能够将VLAN配置信息通告给邻居交换机,这样做可以使工程师只在一台交换机上配置VLAN,同一个VTP域中的所有其他交换机动态学习这些VLAN信息.VTP通告VLAN ID.VLAN 名称和 VL ...