题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3377


简单路径要求权值最大,那么为了回避括号序列单独插头的情况特判多,考虑使用最小表示法。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 21
#define inf ((0x7fffffff)-1)
#define llg int
#define sizee 233
#define maxnZT ((1<<20)+1)
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,code[maxn],zt[][maxnZT],v[][maxnZT],g[][],size[],now,ne,ans=inf*-;
llg maze[][],val[][],ch[];
void outcode(llg x){for (llg i=;i<=m;i++) code[i]=x&,x>>=;} struct node
{
llg x,val,pos;
}; vector<node>a[][sizee]; void decode(llg x) {for (llg i=;i<=m;i++) code[i]=x&,x>>=;} void encode(llg p,llg val)
{
for (llg i=;i<=;i++) ch[i]=-;
ch[]=;
llg C=;
for (llg i=;i<=m;i++)
{
if (ch[code[i]]==-) ch[code[i]]=++C;
code[i]=ch[code[i]];
}
llg x=;
for (llg i=;i<=m;i++) x+=code[i]*(<<(i*));
llg wz=x%sizee,E=a[p][wz].size();
for (llg i=;i<E;i++)
if (a[p][wz][i].x==x)
{
a[p][wz][i].val=max(a[p][wz][i].val,val);
v[p][a[p][wz][i].pos]=max(v[p][a[p][wz][i].pos],val);
return ;
}
size[p]++;
node NEW; NEW.x=x; NEW.val=val; NEW.pos=size[p];
a[p][wz].push_back(NEW);
zt[p][size[p]]=x; v[p][size[p]]=val;
} void init_a(llg p){for (llg i=;i<sizee;i++) a[p][i].clear(); size[p]=;} void shift()///换行 移位
{
for(int i=m;i>;i--)
code[i]=code[i-];
code[]=;
} void dp(llg x,llg y)
{
llg left,up,V;
for (llg K=;K<=size[now];K++)
{
decode(zt[now][K]);
left=code[y-],up=code[y];//左上插头
V=v[now][K];
//------------------------------------------------------------
if ((x== && y==))//开始位置
{
if (maze[x][y+])//往左走
{
code[y]=,code[y-]=;
encode(ne,V+val[x][y]);
}
if (maze[x+][y])//往下走
{
code[y-]=,code[y]=;
encode(ne,V+val[x][y]);
}
continue;
}
//------------------------------------------------------------
if (x==n && y==m)//结束位置
{
if ((!left && up) || (left && !up))//必须有且仅有一个插头
{
code[y]=code[y-]=;
bool pd=true;
for (llg t=;t<=m;t++) if (code[t]) pd=false;
if (pd) ans=max(ans,V+val[x][y]);
}
continue;
}
//------------------------------------------------------------
if (left && up)//插头合并
{
if (left!=up)//如果属于同一个连通块则非法(因为形成了环)
{
code[y]=code[y-]=;
for (llg t=;t<=m;t++) if (code[t]==up) code[t]=left;
if (y==m) shift();
encode(ne,V+val[x][y]);
}
continue;
}
//------------------------------------------------------------
if (left || up)//延续原来的连通分量
{
llg tmp;
if (left) tmp=left;else tmp=up;
if (maze[x][y+])
{
code[y-]=,code[y]=tmp;
encode(ne,V+val[x][y]);
}
if (maze[x+][y])
{
code[y-]=tmp,code[y]=;
if (y==m) shift();
encode(ne,V+val[x][y]);
}
}
//------------------------------------------------------------
if (!left && !up)//没有插头,新建连通分量或者不走这一个格子
{
if (maze[x][y+] && maze[x+][y])
{
code[y]=code[y-]=;
encode(ne,V+val[x][y]);
}
code[y]=code[y-]=;
if (y==m) shift();
encode(ne,V);
}
}
} void work()
{
now=,ne=;
encode(now,);
for (llg i=;i<=n;i++)
{
for (llg j=;j<=m;j++)
{
ne=now^;
init_a(ne);
dp(i,j);
now=ne;
}
}
} void init()
{
memset(maze,,sizeof(maze));
for (llg i=;i<=n;i++)
for (llg j=;j<=m;j++)
scanf("%d",&val[i][j]),maze[i][j]=;
} int main()
{
yyj("hdu3377");
llg cas=;
while (scanf("%d%d",&n,&m)!=EOF)
{
ans=inf*-;
cas++;
init();
if (n== && m==)
{
printf("Case %d: %d\n",cas,val[n][m]);
init_a(),init_a();
continue;
}
work();
printf("Case %d: %d\n",cas,ans);
init_a(),init_a();
}
return ;
}

HDU3377 Plan的更多相关文章

  1. 2019.01.23 hdu3377 Plan(轮廓线dp)

    传送门 题意简述:给一个n*m的带权矩阵,求从左上角走到右下角的最大分数,每个格子只能经过最多一次,n,m≤9n,m\le9n,m≤9. 思路: 考虑轮廓线dpdpdp,但这道题并没有出现回路的限制因 ...

  2. 测试计划(Test Plan)

    测试计划(Test Plan) 版权声明:本文为博主原创文章,未经博主允许不得转载. 测试计划的概念: 测试计划是一个文档,描述了进行测试的测试范围,测试策略和方法,测试资源和进度.是对整个测试活动进 ...

  3. SQL Tuning 基础概述02 - Explain plan的使用

    1.explain plan的使用 SQL> explain plan for delete from t_jingyu; Explained. SQL> select * from ta ...

  4. POJ2175 Evacuation Plan

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4617   Accepted: 1218   ...

  5. New Plan!

    很久无写过blogs,荒废得差不多了,在博客园虽开bolg 5年多,但由于自己工作的问题,从开始的热情记录,到冷却冰冻,再到现在重拾起来,有一番感受:从大学刚毕业的制作网页菜鸟,开始接触DIV,CSS ...

  6. 分析oracle的执行计划(explain plan)并对对sql进行优化实践

    基于oracle的应用系统很多性能问题,是由应用系统sql性能低劣引起的,所以,sql的性能优化很重要,分析与优化sql的性能我们一般通过查看该sql的执行计划,本文就如何看懂执行计划,以及如何通过分 ...

  7. 【转】Oracle 执行计划(Explain Plan) 说明

    转自:http://blog.chinaunix.net/uid-21187846-id-3022916.html       如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQ ...

  8. MySQL慢查询Explain Plan分析

    Explain Plan 执行计划,包含了一个SELECT(后续版本支持UPDATE等语句)的执行 主要字段 id 编号,从1开始,执行的时候从大到小,相同编号从上到下依次执行. Select_typ ...

  9. Timusoj 1982. Electrification Plan

    http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...

随机推荐

  1. JustOj 2038: 叶神的字符串

    题目描述 众所周知,ACS协会会长叶神学习特别好,算法能力也强,作为一个弱渣的大豪哥特别崇拜叶神,觉得‘Y’‘S’这两个字符特别厉害,所以大豪哥的一个键盘上就只有Y,S两个键,大豪哥用这个键盘打出了一 ...

  2. [转载]Request、Request.Form和Request.QueryString的区别

    Request.Request.Form和Request.QueryString的区别 request本身是一个系统的静态对象,本身也可以作为数组调用,比如request("abc" ...

  3. Dubbox分布式框架之入门

    Dubbox简介 Dubbox是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbox,被国内电商及换联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护, ...

  4. django外使用django ORM

    import os, sys import django BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # 定位到你的django根目录 ...

  5. 树莓派无显示器开启ssh的方法

    在boot根目录新建一个名为 ssh 的空文件即可. boot目录所在分区是fat32格式,可以被windows识别和操作 带有系统的tf卡(或SD卡)插入读卡器中,新建ssh文件即可,注意无后缀名

  6. 面试必问:Spring循环依赖的三种方式

    引言:循环依赖就是N个类中循环嵌套引用,如果在日常开发中我们用new 对象的方式发生这种循环依赖的话程序会在运行时一直循环调用,直至内存溢出报错.下面说一下Spring是如果解决循环依赖的. 第一种: ...

  7. Centos7编译安装zabbix-4.0.1

    架构组合:nginx1.9.10+php7.0.32+mysql5.7.22+zabbix4.0.1 nginx1.9.10 先装依赖 openssl-1.1.0f tar zxvf openssl- ...

  8. 【Python028--引入文件】

    一.打开文件 1.open()函数 打开模式 执行操作 ‘r’ 以只读方式打开文件(默认) ‘w’ 以写入的方式打开文件,会覆盖已存在的文件 ‘x’     如果文件已经存在,使用此模式打开将引发异常 ...

  9. iOS字体大小

    1,iOS 字体大小单位是pt——磅. 英文字体的1磅,相当于1/72 英寸,约等于1/2.8mm. px:相对长度单位.像素(Pixel).(PS字体) pt:绝对长度单位.点(Point).(iO ...

  10. gcc,g++

    什么是gcc / g++ 首先说明:gcc 和 GCC 是两个不同的东西 GCC:GNU Compiler Collection(GUN 编译器集合),它可以编译C.C++.JAV.Fortran.P ...