HDU3377 Plan
题目链接: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的更多相关文章
- 2019.01.23 hdu3377 Plan(轮廓线dp)
传送门 题意简述:给一个n*m的带权矩阵,求从左上角走到右下角的最大分数,每个格子只能经过最多一次,n,m≤9n,m\le9n,m≤9. 思路: 考虑轮廓线dpdpdp,但这道题并没有出现回路的限制因 ...
- 测试计划(Test Plan)
测试计划(Test Plan) 版权声明:本文为博主原创文章,未经博主允许不得转载. 测试计划的概念: 测试计划是一个文档,描述了进行测试的测试范围,测试策略和方法,测试资源和进度.是对整个测试活动进 ...
- SQL Tuning 基础概述02 - Explain plan的使用
1.explain plan的使用 SQL> explain plan for delete from t_jingyu; Explained. SQL> select * from ta ...
- POJ2175 Evacuation Plan
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4617 Accepted: 1218 ...
- New Plan!
很久无写过blogs,荒废得差不多了,在博客园虽开bolg 5年多,但由于自己工作的问题,从开始的热情记录,到冷却冰冻,再到现在重拾起来,有一番感受:从大学刚毕业的制作网页菜鸟,开始接触DIV,CSS ...
- 分析oracle的执行计划(explain plan)并对对sql进行优化实践
基于oracle的应用系统很多性能问题,是由应用系统sql性能低劣引起的,所以,sql的性能优化很重要,分析与优化sql的性能我们一般通过查看该sql的执行计划,本文就如何看懂执行计划,以及如何通过分 ...
- 【转】Oracle 执行计划(Explain Plan) 说明
转自:http://blog.chinaunix.net/uid-21187846-id-3022916.html 如果要分析某条SQL的性能问题,通常我们要先看SQL的执行计划,看看SQ ...
- MySQL慢查询Explain Plan分析
Explain Plan 执行计划,包含了一个SELECT(后续版本支持UPDATE等语句)的执行 主要字段 id 编号,从1开始,执行的时候从大到小,相同编号从上到下依次执行. Select_typ ...
- Timusoj 1982. Electrification Plan
http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 sec ...
随机推荐
- 进程表示之进程ID号
UNIX进程总是会分配一个号码用于在其命名空间总唯一地标识它们,该号码称作进程ID号,简称PID. 1.进程ID 但每个进程除了PID外,还有其他的ID,有下列几种可能的类型: (1)处于某个线程组中 ...
- springboot打war包需要注意事项
1. pom文件 1.1 添加servlet-api依赖: <!-- 添加servlet-api的依赖--> <dependency> <groupId>org.a ...
- Cookie,Session,正则表达式
一.Cookie和Session基础知识 Cookie:客户端本地存储的键值对 Http访问是不记录状态的,所以要借助session和cookie来保存访问状态 当你在浏览网站的时候,WEB 服务器 ...
- Oracle误删除数据恢复
select * from tablename as of timestamp to_timestamp('2018-05-04 13:30:00','yyyy-MM-dd hh24:mi:ss') ...
- yii2项目中运行composer 过程中遇到的问题
问题1: Your requirements could not be resolved to an installable set of packages 则表明 未安装fxp/composer-a ...
- Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源
Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源 在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等.今天就给大家介绍一个 Spri ...
- ora-12705解决方法
最近使用instant sqlplus测试时,遇到ora-12705,一开始以为是少了配置,经查是,NLS_LANG设置问题,设置为"SIMPLIFIED CHINESE_CHINA.ZHS ...
- 鸡兔同笼问题(Java)
问题描述:编程解决鸡兔同笼问题,笼子中鸡兔共有35只,94只脚,求有鸡和兔各有几只 我的代码: /** * 鸡兔同笼问题 * @author Administrator * */ public cla ...
- android之发送Get或Post请求至服务器接口
import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; imp ...
- maven的使用记录
maven的使用记录 使用的版本为3.6.0. maven配置部署项目 在cmd命令行中切换到Maven项目的根目录,比如:D:/xxxwork/java/maven-test,然后执行命令:$ mv ...