【题意分析】

给定一张网格图,每个网格可能是普通点、特殊点或障碍点,每个特殊点有一个分值。要求选定一条只经过普通点的可重复回路,使回路内部的特殊点分值和最大。

【算法分析】

  引理:射线法

对于平面内任意一点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题解的更多相关文章

  1. 【BZOJ1294】[SCOI2009]围豆豆(动态规划,状压)

    [BZOJ1294][SCOI2009]围豆豆(动态规划,状压) 题面 BZOJ 洛谷 题解 首先考虑如何判断一个点是否在一个多边形内(不一定是凸的),我们从这个点开始,朝着一个方向画一条射线,看看它 ...

  2. 【BZOJ1294】[SCOI2009]围豆豆Bean 射线法+状压DP+SPFA

    [BZOJ1294][SCOI2009]围豆豆Bean Description Input 第一行两个整数N和M,为矩阵的边长. 第二行一个整数D,为豆子的总个数. 第三行包含D个整数V1到VD,分别 ...

  3. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  4. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  5. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  6. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  7. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  8. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  9. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

随机推荐

  1. Android项目中实现native调用

    转载自搜狗测试公众号,本人学习使用,侵权删 最近小编在做公司输入法项目中java与native交互部分的测试,先简单学习了java代码调用native代码的实现原理,本次与大家一起分享jni协议,了解 ...

  2. vue 本地环境API代理设置和解决跨域

    写一个config.js文件,作为项目地址的配置. //项目域名地址 const url = 'https://exaple.com'; let ROOT; //由于封装的axios请求中,会将ROO ...

  3. css自适应问题

    1. 图片自适应 <img src="app-logo.png" srcset="app-logo.png 175w,app-logo-double.png 365 ...

  4. leetcode-168周赛-1297-子串的最大出现次数

    题目描述: 自己的提交: class Solution: def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) ...

  5. C# 16进制转字符串,字符串转16进制

    { //========================================================== //16进制转字符串 public static byte[] HexTo ...

  6. Vue学习笔记【18】——Vue中的动画(使用过渡类名)

    为什么要有动画:动画能够提高用户的体验,帮助用户更好的理解页面中的功能: 使用过渡类名 步骤分析  需求: 点击按钮,让 h3 显示,再点击,让 h3 隐藏  1. 使用 transition 元素, ...

  7. 用javascript插入<style>样式

    function addCSS(cssText){ var style = document.createElement('style'), //创建一个style元素 head = document ...

  8. LInux多线程编程----线程属性pthread_attr_t

    1.每个POSIX线程有一个相连的属性对象来表示属性.线程属性对象的类型是pthread_attr_t,pthread_attr_t 在文件/usr/include/bits/pthreadtypes ...

  9. fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit

    最近想用一下Xtreme ToolkitPro 界面库,安装后用VC6根据向导 产生一个工程,编译时出现如下的错误: fatal error C1076: compiler limit : inter ...

  10. Spellchecker inspection helps locate typos and misspelling in your code, comments and literals, and fix them in one click

    Pycharm设置 Pycharm总是很多的拼写检查波拉线 Spellchecker inspection helps locate typos and misspelling in your cod ...