POJ 3740 精确覆盖 模板
2014-05-15 20:55
274 查看
//#pragma comment(linker, "/STACK:102400000,102400000") #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<cmath> #include<cctype> #include<string> #include<algorithm> #include<iostream> #include<ctime> #include<map> #include<set> using namespace std; #define MP(x,y) make_pair((x),(y)) #define PB(x) push_back(x) typedef __int64 LL; //typedef unsigned __int64 ULL; /* ****************** */ const int INF=1000111222; const double INFF=1e100; const double eps=1e-8; //const LL mod=1000000007; const int NN=305; const int MM=2000010; /* ****************** */ const int maxn=305; const int maxr=20; const int maxnode=305*20; int sz; int ans[maxr],ansd; int S[maxn],dl_qw[maxn]; int row[maxnode],col[maxnode]; int L[maxnode],R[maxnode],U[maxnode],D[maxnode]; void dl_init(int n) { int i; for(i=0;i<=n;i++) { U[i]=D[i]=i; L[i]=i-1; R[i]=i+1; S[i]=0; } L[0]=n; R =0; sz=n+1; } /* cnt>0!!!! */ void dl_add(int r,int cnt) { int i,first=sz; for(i=0;i<cnt;i++) { int c=dl_qw[i]; L[sz]=sz-1; R[sz]=sz+1; U[sz]=U[c]; D[sz]=c; D[ U[c] ]=sz; U[c]=sz; row[sz]=r; col[sz]=c; S[c]++; sz++; } L[first]=sz-1; R[sz-1]=first; } /* 删除c列 */ /* L,R只用删最上面的就好,其他点删U,D就好了 */ void remove(int c) { int i,j; L[R[c]]=L[c]; R[L[c]]=R[c]; for(i=D[c];i!=c;i=D[i]) for(j=R[i];j!=i;j=R[j]) { U[D[j]]=U[j]; D[U[j]]=D[j]; --S[col[j]]; } } /* 与remove顺序是相反的 */ void restore(int c) { int i,j; for(i=U[c];i!=c;i=U[i]) for(j=L[i];j!=i;j=L[j]) { ++S[col[j]]; U[D[j]]=j; D[U[j]]=j; } L[R[c]]=c; R[L[c]]=c; } bool dl_dfs(int d) { if(R[0]==0) { ansd=d; return true; } int i,j,c=R[0]; for(i=R[0];i!=0;i=R[i]) if(S[i]<S[c]) c=i; remove(c); for(i=D[c];i!=c;i=D[i]) { ans[d]=row[i]; for(j=R[i];j!=i;j=R[j]) remove(col[j]); if(dl_dfs(d+1)) return true; for(j=L[i];j!=i;j=L[j]) restore(col[j]); } restore(c); return false; } int main() { int n,m,i,j,toll,t; while(scanf("%d%d",&n,&m)!=EOF) { dl_init(m); for(i=1;i<=n;i++) { toll=0; for(j=1;j<=m;j++) { scanf("%d",&t); if(t) { dl_qw[toll++]=j; } } if(toll>0) dl_add(i,toll); } if(dl_dfs(0)) puts("Yes, I found it"); else puts("It is impossible"); } return 0; }
相关文章推荐
- POJ 3740 DLX 精确覆盖模板题
- POJ 3740 Easy Finding(DLX精确覆盖裸题)
- POJ-3740-Easy Finding【DLX精确覆盖】
- poj - 3740 - Easy Finding(精确覆盖DLX)
- poj 3740 Easy Finding(Dancing Links 精确覆盖)
- LA_2659_POJ_3076_ZOJ_3122_Sudoku(DancingLinksX精确覆盖,数独题模板)
- POJ_3740——精确覆盖问题,DLX模版
- poj 3740 DLX(精确覆盖)
- poj 3740 Easy Finding 精确覆盖
- 【POJ】3740 Easy Finding 精确覆盖入门题
- hust 1017 dancing links 精确覆盖模板题
- DLX模板之精确覆盖和重复覆盖
- 最新版dlx模板(精确覆盖+重复覆盖)
- (简单) POJ 3076 Sudoku , DLX+精确覆盖。
- poj 3076 Sudoku 数独(16*16)+精确覆盖+DLX 只输出一组解
- HUST 1017 Exact cover (精确覆盖|Dancing Links模板题)
- POJ 3041 Asteroids(最小顶点覆盖,匈牙利算法模板)
- POJ 2676 锻炼码力:数独,精确覆盖的DLX
- HUST_1017_ExactCover(DancingLinksX精确覆盖模板题)
- POJ 3041 Asteroid (二分匹配模板-二分图最小覆盖)