题目链接: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. Ubuntu 为 root 帐号开启 SSH 登录

    1. 修改 root 密码sudo passwd root 2. 以其他账户登录,通过 sudo nano 修改 /etc/ssh/sshd_config :xxx@ubuntu:~$ su - ro ...

  2. <转>jmeter(十)参数化

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  3. MVC 部署HTTPS网站

    一.项目配置 什么是全站HTTPS 全站HTTPS就是指整个网站的所有页面,所有资源全部使用HTTPS链接.当用户的某个请求是明文的HTTP时,应该通过HTTP状态码301永久重定向到对应的HTTPS ...

  4. Autel MaxiSys Elite Diagnostic Tool Common problem solving methods

    1. updating MaxiFlash Elite to firmware 3.21? My maxisys communicate with the MaxiFlash J2534 but Ma ...

  5. Mysql初级第二天(wangyun)

    SQL 1.LIKE 操作符 SELECT 列名称 FROM 表名称 WHERE 列 LIKE 值('N%'/'%N%'/'%N','N_') SELECT 列名称 FROM 表名称 WHERE 列 ...

  6. MySQL字符类型datetime与timestamp

    这片博客来详细分区一下这哥俩! 首先来说明这两个字符类型: DATETIME 8 1000-01-01 00:00:00 ~9999~12-31 23:59:59 0000-00-00 00:00:0 ...

  7. leetcode [34] Find First and Last Position of Element in Sorted Array

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  8. mongodb三种引擎测试(转)

    文章http://diyitui.com/content-1459560904.39084552.html亲测了根据证券行情存储的性能情况,我们目前使用load local infile,平均每秒更新 ...

  9. ThirdAPI

    //public class ThirdAPI //{ // [DllImport("ThirdAPI.dll")] // public static extern int Ini ...

  10. build custom centos7

    必读,在以下内容之前. pre: 0. install log redhat6 mak iso guide redhat7 make iso guide 1. Linux安装>(三)发行版制作 ...