http://poj.org/problem?id=3133

题意:n×m的网格,有2个2,2个3,他们不会重合。还有障碍1。现在求2到2的路径和3到3的路径互不相交的最短长度-2。(2<=n,m<=9)

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct H {
static const int M=1000007;
struct E { int next, to; }e[M<<1];
int mn[M], hash[M], cnt, ihead;
H() { cnt=ihead=0; memset(mn, 0x3f, sizeof mn); memset(hash, -1, sizeof hash); }
bool find(int x, int &pos) {
pos=x%M;
while(1) { if(hash[pos]==x) return 0; if(hash[pos]==-1) break; ++pos; if(pos==M) pos=0; }
hash[pos]=x;
return 1;
}
void ins(int a, int b) {
int pos; if(!find(a, pos)) { mn[pos]=min(mn[pos], b); return; }
mn[pos]=b; e[++cnt].next=ihead; ihead=cnt; e[cnt].to=pos;
}
void clr() { for(int i=ihead; i; i=e[i].next) hash[e[i].to]=-1, mn[e[i].to]=0x3f3f3f3f; ihead=cnt=0; }
}T[2];
int n, m, mp[12][12], all;
#define BIT(a,b) ((a)<<((b)<<1))
#define CLR(a,b) (a^=(a&BIT(3,b)))
#define GET(a,b) (3&((a)>>((b)<<1))) bool next(int s, int row, int col, bool U, bool D, bool L, bool R, int id, int &t) {
if((!row&&U) || (!col&&L) || (row==n-1&&D) || (col==m-1&&R)) return 0;
if((D&&mp[row+1][col]==1) || (R&&mp[row][col+1]==1)) return 0;
int l=GET(s, col), u=GET(s, col+1), d=0, r=0;
if((L&&!l) || (!L&&l) || (U&&!u) || (!U&&u)) return 0;
if((l&&l!=id) || (u&&u!=id)) return 0;
t=s;
if(!l && !u) { if(D) d=id; if(R) r=id; }
else if(l && u) { if(l!=u || mp[row][col]>=1) return 0; }
else if(l && !u) { if(D) d=l; if(R) r=l; }
else if(!l && u) { if(D) d=u; if(R) r=u; }
CLR(t, col); CLR(t, col+1);
t|=BIT(d, col); t|=BIT(r, col+1);
if(col==m-1) t<<=2; t&=all;
return 1;
} int bfs() {
int now=0, last=1;
T[now].clr();
T[now].ins(0, 0);
for(int row=0; row<n; ++row) for(int col=0; col<m; ++col) {
T[last].clr();
for(int i=T[now].ihead; i; i=T[now].e[i].next) {
int t, s=T[now].hash[T[now].e[i].to], d=T[now].mn[T[now].e[i].to]+1, id=mp[row][col]-1;
if(id>=1) {
if(next(s, row, col, 1, 0, 0, 0, id, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 1, 0, 0, id, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 0, 1, 0, id, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 0, 0, 1, id, t)) T[last].ins(t, d);
}
else {
if(next(s, row, col, 0, 0, 0, 0, 0, t)) T[last].ins(t, d-1);
if(id==-1) {
for(int k=1; k<=2; ++k) {
if(next(s, row, col, 1, 1, 0, 0, k, t)) T[last].ins(t, d);
if(next(s, row, col, 1, 0, 1, 0, k, t)) T[last].ins(t, d);
if(next(s, row, col, 1, 0, 0, 1, k, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 1, 1, 0, k, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 1, 0, 1, k, t)) T[last].ins(t, d);
if(next(s, row, col, 0, 0, 1, 1, k, t)) T[last].ins(t, d);
}
}
}
}
swap(now, last);
}
int ret=0x3f3f3f3f;
for(int i=T[now].ihead; i; i=T[now].e[i].next) ret=min(ret, T[now].mn[T[now].e[i].to]);
return ret>=0x3f3f3f3f?0:ret-2;
} int main() {
while(scanf("%d%d", &n, &m), n|m) {
for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) scanf("%d", &mp[i][j]);
all=BIT(1, m+1)-1;
printf("%d\n", bfs());
}
return 0;
}

  


吐槽:

1、改着改着自己本来特判的东西然后改掉了QAQ,然后造数据对拍才发现....

2、为什么我写的插头dp常数那么大啊!!!

本题比简单回路简单多了吧?

自己很容易可以画出左插头和上插头的情况,然后在位中维护的是当前插头所连接的标号然后就行啦...

【POJ】3133 Manhattan Wiring的更多相关文章

  1. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  2. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  3. 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉

    DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $  当然这里的$i$和$k$都是偶数啦~ ...

  4. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  5. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

  6. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

  7. 【POJ】【1637】Sightseeing tour

    网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...

  8. 【poj】1001

    [题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...

  9. 【POJ】3070 Fibonacci

    [算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...

随机推荐

  1. ****php:require_once(dirname(__FILE__)."/./config_uc.php");

    Q:麻烦清楚地讲解一下这句的意思,具体路径是怎样的,这个文解在 根目录,如果我想放在根目录下的tieba文件夹里,应该怎么修改/./ 这个是表示什么? A: require_once(dirname( ...

  2. ASP.NET Web API 全局权限和全局异常处理

    在开发中,我使用json格式序列化,所以将默认的xml序列化移除 public static class WebApiConfig { public static void Register(Http ...

  3. SQL语法中的JOIN类型

    这个要弄明白哟..CROSS JOIN, NATURAL, INNER JOIN ,LEFT OUTER JOIN(LEFT JOIN) 等等....带LEFT,RIGHT的必为OUTER,所以OUT ...

  4. 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)

    Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件. 1.相关文 ...

  5. windows操作技巧

    -------------------win7专有------------------- WIN+上下左右可以将当前窗口 最大化.左侧放置.右侧放置.最小化 WIN+Shift+左右是让窗口在多个屏幕 ...

  6. WPF 创建自定义窗体

    在前面的一篇博客"WPF 自定义Metro Style窗体",展示了如何创建一个类似于Metro Style的Window,并在程序中使用.但是这个窗体不能够自由的改变大小.今天的 ...

  7. WPF QuickStart系列之附加属性(Attached Property)

    这一篇博客是关于如何使用附加属性和创建自定义附加属性的. 1. 附加属性使用, WPF中对附加属性使用最多的莫过于对控件布局时设置控件的位置,例如在Canvas中有一个Rectangle, Ellip ...

  8. Windows+Git+TortoiseGit+COPSSH 安装图文教程

    转自:http://blog.csdn.net/aaron_luchen/article/details/10498181 准备工作: 1. Git-1.8.1.2-preview20130201.e ...

  9. 【转】Hive配置文件中配置项的含义详解(收藏版)

    http://www.aboutyun.com/thread-7548-1-1.html 这里面列出了hive几乎所有的配置项,下面问题只是说出了几种配置项目的作用.更多内容,可以查看内容问题导读:1 ...

  10. hive脚本出现Error: java.lang.RuntimeException: Error in configuring object和Caused by: java.lang.IndexOutOfBoundsException: Index: 9, Size: 9

    是在reduce阶段报的错误,详细错误信息是 朱传豪 19:04:48 Diagnostic Messages for this Task: Error: java.lang.RuntimeExcep ...