poj3740 Easy Finding DLX
来源:互联网 发布:每天最佳健身时间 知乎 编辑:程序博客网 时间:2024/06/12 01:47
跟上题差不多,还是一个01矩阵,问是否可以选出若干行使得每列恰有一个1。模型都给建好了,直接建表搜就可以了..
#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>using namespace std;typedef long long ll;const int maxn=320*320;struct DLX{ int col[maxn],row[maxn],ans[maxn]; int S[maxn],H[maxn]; int size,cnt; int U[maxn],R[maxn],D[maxn],L[maxn]; void remove(int c) { L[R[c]]=L[c]; R[L[c]]=R[c]; for (int i=D[c]; i!=c; i=D[i]) { for (int j=R[i]; j!=i; j=R[j]) { U[D[j]]=U[j]; D[U[j]]=D[j]; --S[col[j]]; } } } void resume(int c) { L[R[c]]=c; R[L[c]]=c; for (int i=U[c]; i!=c; i=U[i]) { for (int j=R[i]; j!=i; j=R[j]) { U[D[j]]=j; D[U[j]]=j; ++S[col[j]]; } } } bool dance(int k) { int c=R[0]; if (c==0) { cnt=k; return true; } for (int i=R[0]; i!=0; i=R[i]) { if (S[c]>S[i]) c=i; } remove(c); for (int i=D[c]; i!=c; i=D[i]) { ans[k]=row[i]; for (int j=R[i]; j!=i; j=R[j]) remove(col[j]); if (dance(k+1)) return true; for (int j=R[i]; j!=i; j=R[j]) resume(col[j]); } resume(c); return false; } void init(int n) { for (int i=0; i<=n; i++) { S[i]=0; L[i]=i-1; R[i]=i+1; U[i]=D[i]=i; } L[0]=n; R[n]=0; size=n+1; memset(H,-1,sizeof H); } void link(int x,int y) { ++S[col[size]=y]; row[size]=x; D[size]=D[y]; U[D[y]]=size; U[size]=y; D[y]=size; if (H[x]<0) H[x]=L[size]=R[size]=size; else { R[size]=R[H[x]]; L[R[H[x]]]=size; L[size]=H[x]; R[H[x]]=size; } size++; }}dlx;int n,m,k,num;int main(){// freopen("in.txt","r",stdin); while (~scanf("%d%d",&m,&n)) { dlx.init(n); for (int i=1; i<=m; i++) for (int j=1; j<=n; j++) { scanf("%d",&k); if (k) { dlx.link(i,j); } } if (dlx.dance(0)) { puts("Yes, I found it"); } else puts("It is impossible"); } return 0;}
0 0
- poj3740 Easy Finding DLX
- POJ3740 Easy Finding 舞蹈链 DLX
- 【POJ3740】Easy Finding DLX(Dancing Links)精确覆盖问题
- poj3740 Easy Finding(深搜)
- POJ3740--Easy Finding
- POJ3740-Easy Finding
- POJ3740 Easy Finding(Dancing Links)
- POJ3740--Easy Finding(Dancing Links)
- 【POJ3740】Easy Finding,解题报告+思路+代码
- POJ 3740 Easy Finding (DLX模板)
- POJ-3740-Easy Finding【DLX精确覆盖】
- poj - 3740 - Easy Finding(精确覆盖DLX)
- [ACM] POJ 3740 Easy Finding (DLX模板题)
- POJ 3740 Easy Finding(DLX精确覆盖裸题)
- POJ3740Easy Finding(DLX入门)
- poj3740
- POJ3740
- POJ3740
- UITableView 滚动流程性优化
- 每日学习心得:$.extend()方法和(function($){...})(jQuery)详解
- PowerDesigner导出mysql脚本里包含视图
- macbook装机注意事项&&双系统崩溃解决办法
- 获取android应用签名证书(打包APK用到的那个文件)的SHA1,MD5,SHA256值
- poj3740 Easy Finding DLX
- MySQL server has gone away报错原因分析
- UVA - 1372 Log Jumping
- hduoj-1051-Wooden Sticks
- GDB调试之TUI界面
- 23-Linux-软件包管理-YUM
- UVA 10534 Wavio Sequence (双向LIS)
- Javascript Function 对象 属性和方法
- Dojo 学习笔记–dojo模块引用 和上下文绑定