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. MVC授权认证

    处于安全性考虑,MVC可以完成授权认证,授权认证的方式如下: 1.配置Config文件,设置登录页面: <authentication mode="Forms"> &l ...

  2. AOJ673 聪明的输入法(字典树)

    #include<cstdio> #include <cstdlib> #include <cstring> #include <iostream> # ...

  3. 验证备份前设置CONFIGURE CONTROLFILE AUTOBACKUP ON/OFF; 的区别

    关于rman的,环境: oracle 10.2.0 rman nocatalog方式 1.首先设置 CONFIGURE CONTROLFILE AUTOBACKUP ON; 然后进行数据库全备份 RM ...

  4. android 入门-ID

    @+id/btn             //表示在R.java文件里面新增一个id为btn的控件索引,最常用的一种声明控件id的方式. @+android:id/list  //+android:表 ...

  5. 提高WPF程序性能的几条建议

    这篇博客将介绍一些提高WPF程序的建议(水平有限,如果建议有误,请指正.) 1. 加快WPF程序的启动速度: (1).减少需要显示的元素数量,去除不需要或者冗余的XAML元素代码. (2).使用UI虚 ...

  6. python学习第三天

    小结: 总体上,python是解释型语言,开源比较好,速度较慢,装逼神器,解释器较常用的是CPython,安装后python进入运行环境 exit()退出 第一个hello world : print ...

  7. Redis在Windows下的安装和使用

    NoSQL简介 介绍redis前,我想还是先认识下NoSQL,即not only sql, 是一种非关系型的数据存储,key/value键值对存储.现有Nosql DB 产品: Redis/Mongo ...

  8. hdfs 名称节点和数据节点

    名字节点(NameNode )是HDFS主从结构中主节点上运行的主要进程,它指导主从结构中的从节点,数据节点(DataNode)执行底层的I/O任务. 名字节点是HDFS的书记员,维护着整个文件系统的 ...

  9. 【POI xls】解析xls遇到的问题

    问题1:Package should contain a content type part org.apache.poi.POIXMLException: org.apache.poi.openxm ...

  10. Eclipse首字母快捷设置

    windows->Preference  ,然后: