bzoj1294题解
【题意分析】
给定一张网格图,每个网格可能是普通点、特殊点或障碍点,每个特殊点有一个分值。要求选定一条只经过普通点的可重复回路,使回路内部的特殊点分值和最大。
【算法分析】
引理:射线法
对于平面内任意一点P,向x轴方向引一条射线l,若l与一封闭曲线m的交点数为奇,则P必定在封闭曲线m所围成的封闭图形内,反之则P必定在其外。
考虑状态压缩DP,f[i][j][k]表示当前在点(i,j)并且豆豆的二进制状态为k时获得的最大分值。由于此DP的阶段比较特殊,故需要用SPFA进行转移。
【参考代码】
#pragma GCC optimize(2)
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <functional>
#include <vector>
#define REP(i,low,high) for(register int i=(low);i<=(high);++i)
#define PER(i,high,low) for(register int i=(high);i>=(low);--i)
using namespace std; //ex_cmp {
template<typename T,class Compare> inline bool getcmp(T &target,const T &pattern,Compare comp)
{
return comp(pattern,target)?target=pattern,:;
}
//} ex_cmp static const int N=,dx[]={,,,-},dy[]={,-,,}; vector<int> line[];
static int n,m,d,ans=,dfn=; bool map[][]={},inq[][][];
int val[],bx[],qx[N],qy[N],qz[N],f[][][],vis[][][]={};
inline int &move(int &x) {return ++x==N?x=:x;}
inline bool cmp(const int &one,const int &another) {return bx[one]>bx[another];}
inline void SPFA(const int &sx,const int &sy)
{
vis[sx][sy][f[qx[]=sx][qy[]=sy][qz[]=]=]=++dfn; for(int head=-,tail=;head!=tail;)
{
move(head); int frx=qx[head],fry=qy[head],frz=qz[head];
if(frx==sx&&fry==sy) getcmp(ans,f[frx][fry][frz],greater<int>()); REP(i,,)
{
int tox=frx+dx[i],toy=fry+dy[i]; if(tox&&toy&&tox<=n&&toy<=m&&!map[tox][toy])
{
int toz=frz,det=; if(i<)
{
int x,y; toy>fry?(x=tox,y=toy):(x=frx,y=fry); PER(j,line[y].size()-,)
{
int dig=line[y][j]; if(bx[dig]>=x) break;
int bin=<<dig-; det+=(toz&bin?-:)*val[dig],toz^=bin;
}
}
if(vis[tox][toy][toz]!=dfn||f[frx][fry][frz]+det->f[tox][toy][toz])
{
f[tox][toy][toz]=f[frx][fry][frz]+det-,vis[tox][toy][toz]=dfn;
if(!inq[tox][toy][toz]) move(tail),inq[qx[tail]=tox][qy[tail]=toy][qz[tail]=toz]=;
}
}
}
inq[frx][fry][frz]=;
}
}
int main()
{
scanf("%d%d%d",&n,&m,&d),memset(line,,sizeof line),memset(f,0xfe,sizeof f); REP(i,,d) scanf("%d",val+i);
REP(i,,n) REP(j,,m)
{
char ch=getchar(); for(;isspace(ch)||ch=='\n';ch=getchar()); int num=ch-''; switch(ch)
{
case '':break; case '#':map[i][j]=; break; default:map[bx[num]=i][j]=,line[j].push_back(num);
}
}
REP(i,,m) sort(line[i].begin(),line[i].end(),cmp); REP(i,,n) REP(j,,m) if(!map[i][j]) SPFA(i,j);
return printf("%d\n",ans),;
}
bzoj1294题解的更多相关文章
- 【BZOJ1294】[SCOI2009]围豆豆(动态规划,状压)
[BZOJ1294][SCOI2009]围豆豆(动态规划,状压) 题面 BZOJ 洛谷 题解 首先考虑如何判断一个点是否在一个多边形内(不一定是凸的),我们从这个点开始,朝着一个方向画一条射线,看看它 ...
- 【BZOJ1294】[SCOI2009]围豆豆Bean 射线法+状压DP+SPFA
[BZOJ1294][SCOI2009]围豆豆Bean Description Input 第一行两个整数N和M,为矩阵的边长. 第二行一个整数D,为豆子的总个数. 第三行包含D个整数V1到VD,分别 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
随机推荐
- HashMap的hash分析
哈希 Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空 ...
- Eclipse maven 明明有jar包 但是不能用
原因1:没有引入pom.xml依赖 解决: 添加pom.xml依赖
- docker安装cboard
参考链接: https://hub.docker.com/r/chenlmdocker/docker-cboard/dockerfile https://www.cnblogs.com/zimo-ji ...
- The 'with' and 'as' Keywords
Programming is all about getting the computer to do the work. Is there a way to get Python to automa ...
- JavaScript事件绑定的常见方式
在Javascript中,事件绑定一共有3种方式: ① 行内绑定 ② 动态绑定 ③ 事件监听 原文: https://mbd.baidu.com/newspage/data/landingsuper? ...
- python判断文件的编码格式是否为UTF8 无BOM格式
转自: https://www.cnblogs.com/ferraborghini/p/4951102.html https://www.cnblogs.com/Detector/p/8744992. ...
- HTML5中的Canvas和SVG
Canvas 和 SVG 都允许我们在浏览器中创建图形,但是它们在根本上是不同的. 1 SVG SVG 是一种使用 XML 描述 2D 图形的语言. SVG 基于 XML,这意味着 SVG DOM 中 ...
- 【NOI2019模拟2019.6.29】组合数(Lucas定理、数位dp)
Description: p<=10且p是质数,n<=7,l,r<=1e18 题解: Lucas定理: \(C_{n}^m=C_{n~mod~p}^{m~mod~p}*C_{n/p} ...
- apue.h报错问题
下载apue.3e后进入make,提示错误如下: collect2: error: exit status Makefile:: recipe for target 'badexit2' failed ...
- Hbase集群类型|集群配置|服务器选型|磁盘容量规划
HBase和Hadoop的集群类型 1.单机模式 主要用于开发工作,一台机器上运行所有的守护进程,或者一台机器运行多个虚拟机.一般用于评估和测试. 2.小型集群 20台机器以内的集群,不同的机器运行不 ...