[JLOI2009]神秘的生物

只需要维护连通情况,采用最小表示法,表示此格是否存在,也即插头是否存在

分情况讨论当前格子的轮廓线上方格子和左方格子状态,转移考虑当前格子选不选,决策后状态最后要能合法

\(\text{Code}\)

$\text{Code}$
#include <bits/stdc++.h>
#define IN inline
using namespace std; const int N = 15;
int bit[N], a[N][N], ans = -2e9, n; struct Hash_Table {
#define mod 590027
struct edge{int nxt, sta, f;}e[1 << 24];
int h[mod + 5], tot;
IN void clear(){tot = 0, memset(h, 0, sizeof h);}
IN void insert(int s, int v) {
int id = s % mod;
for(int i = h[id], x; i; i = e[i].nxt)
if (e[i].sta == s) return e[i].f = max(e[i].f, v), void();
e[++tot] = edge{h[id], s, v}, h[id] = tot;
}
}hs[2]; IN int Recode(int s, int v) {
int vis[8] = {}, cnt = 0, t = 0;
for(int i = 0, x; i < n; i++) {
if (!(x = (s >> 3*i) & 7)) continue;
if (!vis[x]) vis[x] = ++cnt;
t += vis[x] * bit[i];
}
if (cnt == 1) ans = max(ans, v);
return t;
}
IN int Count(int s, int v) {
int res = 0;
for(int i = 0; i < n; i++) if (((s >> 3*i) & 7) == v) ++res;
return res;
} void solve() {
int cur = 0; hs[cur].insert(0, 0);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
hs[cur^1].clear();
for(int k = 1; k <= hs[cur].tot; k++) {
int curS = hs[cur].e[k].sta, curF = hs[cur].e[k].f;
int bd = (curS >> 3*(j - 1)) & 7, br = 0;
if (j > 1) br = (curS >> 3*(j - 2)) & 7;
if (!bd && !br) {
hs[cur^1].insert(Recode(curS, curF), curF);
hs[cur^1].insert(Recode(curS + 7*bit[j-1], curF + a[i][j]), curF + a[i][j]);
}
else if (!bd && br) {
hs[cur^1].insert(Recode(curS, curF), curF);
hs[cur^1].insert(Recode(curS + br*bit[j-1], curF + a[i][j]), curF + a[i][j]);
}
else if (bd && !br) {
hs[cur^1].insert(Recode(curS, curF + a[i][j]), curF + a[i][j]);
if (Count(curS, bd) >= 2) hs[cur^1].insert(Recode(curS - bd*bit[j-1], curF), curF);
}
else {
if (Count(curS, bd) >= 2) hs[cur^1].insert(Recode(curS - bd*bit[j-1], curF), curF);
if (br != bd) for(int w = 0; w < n; w++) if (((curS >> 3*w) & 7) == bd) curS += bit[w] * (br - bd);
hs[cur^1].insert(Recode(curS, curF + a[i][j]), curF + a[i][j]);
}
}
cur ^= 1;
}
} int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) scanf("%d", &a[i][j]), ans = max(ans, a[i][j]);
bit[0] = 1;
for(int i = 1; i <= n; i++) bit[i] = bit[i - 1] << 3;
solve(), printf("%d\n", ans);
}

插头dp 模板的更多相关文章

  1. 插头DP模板

    /* 插头dp模板 抄的GNAQ 的 括号表示法 */ #include<cstdio> #include<algorithm> #include<cstring> ...

  2. bzoj1814 Ural 1519 Formula 1(插头dp模板题)

    1814: Ural 1519 Formula 1 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 924  Solved: 351[Submit][Sta ...

  3. hdu1964之插头DP求最优值

    Pipes Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  4. 【HDU】1693:Eat the Trees【插头DP】

    Eat the Trees Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. 插头dp练习

    最近学了插头dp,准备陆续更新插头dp类练习. 学习论文还是cdq那篇<基于连通性状态压缩的动态规划问题>. 基本的想法都讲得很通透了,接下来就靠自己yy了. 还有感谢kuangbin大大 ...

  6. 学习笔记:插头DP

    基于连通性的状压DP问题. 一般是给你一个网格,有一些连通性的限制. 例题 插头DP模板 链接 题意:网格图,去掉一些点,求哈密顿回路方案数. 一般按格递推(从上到下,从左到右). 每个格子要从四个方 ...

  7. 模板:插头dp

    前言: 严格来讲有关dp的都不应该叫做模板,因为dp太活了,但是一是为了整理插头dp的知识,二是插头dp有良好的套路性,所以姑且还叫做模板吧. 这里先推荐一波CDQ的论文和这篇博客http://www ...

  8. LG5056 【模板】插头dp

    题意 题目背景 ural 1519 陈丹琦<基于连通性状态压缩的动态规划问题>中的例题 题目描述 给出n*m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? 输 ...

  9. P5056 【模板】插头dp

    \(\color{#0066ff}{ 题目描述 }\) 给出n*m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? \(\color{#0066ff}{输入格式}\) 第1 ...

  10. 【模板】插头dp

    题目描述 题解: 插头$dp$中经典的回路问题. 首先了解一下插头. 一个格子,上下左右四条边对应四个插头.就像这样: 四个插头. 一个完整的哈密顿回路,经过的格子一定用且仅用了两个插头. 所以所有被 ...

随机推荐

  1. sublime text设置build system automatic提示no build system

    解决办法: 将: "selector": "source.asm" 改为: "selector": ["source.asm&qu ...

  2. Day31面向对象之魔法方法

    Day31面向对象之魔法方法 类的常用魔法方法如下 序号 双下方法 触发条件 1 init 对象添加独有数据的时候自动触发 2 str 对象被执行打印操作的时候自动触发 3 call 对象加括号调用的 ...

  3. jquery操作class

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 第五章:matplotlib水印和桑基图

    1.Matplotlib水印 1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 x = np.linspace(0.0,10,40) ...

  5. Blazor Server完美实现Cookie Authorization and Authentication

    Blazor server-side application用Microsoft.AspNetCore.Identity.EntityFrameworkCore实现Authorization 和 Au ...

  6. LeetCode HOT 100:搜索旋转排序数组

    题目:33. 搜索旋转排序数组 题目描述: 一个整数数组,数组每个值都不相同,且该整数数组是一个被旋转过的数组.被旋转过的数组是指,由一个递增的数组,从某一个下标开始往后的元素,移到最开头.举个例子: ...

  7. uniapp微信小程序 选择日期时间

    一.根据需要点击选择时间日期,效果如下图: (1)新建一个dateTimePicker.js文件 function withData(param){ return param < 10 ? '0 ...

  8. Flask初步认识

    1.Flask基本认识 Flask 本身相当于一个内核,其他几乎所有的功能都要用到扩展包(数据库Flask-SQLAlchemy),都需要用第三方的扩展来实现.比如可以用 Flask 扩展加入ORM. ...

  9. 【转载】C#使用Dotfuscator混淆代码以及加密

    C#编写的代码如果不进行一定程度的混淆和加密,那么是非常容易被反编译进行破解的,特别是对于一些商业用途的C#软件来说,因为盯着的人多,更是极易被攻破.使用Dotfuscator可以实现混淆代码.变量名 ...

  10. 利用Redisson实现订单关闭

    实体类 为了方便测试,直接在测试类中的写内部类: @Data @AllArgsConstructor @NoArgsConstructor public class OrderInfo { /** * ...