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. Easy UI 面板

    驾考园 http://www.jiakaoyuan.com 驾考园信息网 下载(源码)

  2. C# 使用Conditional特性而不是#if条件编译

    概述 #if/#endif 语句常用来基于同一份源码生成不同的编译结果,其中最常见的就是debug版和release版.但是这些工具在实际应用中并不是非常友好,因为它们容易被滥用,其代码页进而难以理解 ...

  3. 玩转SSRS第十篇---自定义代码

    提到SSRS 那么就不得不提一下自定义代码的功能,通过自定义代码,有时候可以解决一些比较复杂的问题,比如将让指定的数据行应用指定的属性值.此篇将演示如何通过简单结构的自定义代码进行报表样式的基本设计. ...

  4. 攻城狮在路上(壹) Hibernate(五)--- 映射一对多关联关系

    关联是有方向的,包含单向关联和双向关联.分别讨论.本文以客户Customer和订单Order来进行讨论:一个Customer有多个Order,每个Order对应一个Customer. Customer ...

  5. Java Hour 63 反射

    首先要感谢下这位可爱的作者: http://www.cnblogs.com/rollenholt/archive/2011/09/02/2163758.html. 本文是基于其模板的模仿实验. 获得C ...

  6. hdu 4068 福州赛区网络赛H 排列 ***

    拍的太慢了,很不满意 排完序之后,枚举自己和对手状态,若被击败,则再枚举自己下一个策略,直到可以击败对手所有的策略 #include<cstdio> #include<iostrea ...

  7. C#中的Infinity有个小坑

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 昨天家里有事,上网也不方便,就没有推送文章.今天很累,也不长篇大论了.简单介绍一下最近遇到的 ...

  8. Unity3d 提示 "The scripts file name does not match the name of the class defined in the script!"的解决办法

    有两个原因,一个是文件的名称和类名不一致 第二个原因是有命名空间, 排除应该是可以修复的

  9. java Clone使用方法详解

    java"指针"       Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文会试图澄清这一概念.并且由于Java不能 通过 ...

  10. 【框架】网络请求+Gson解析--Retrofit 2

    其实内部是封装了Okhttp和Gson解析 public class CourseFragmentAPI { public static void get(String userId, BaseCal ...