您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: