Problem Description
sevenzero liked Warcraft very much, but he haven't practiced it for several years after being addicted to algorithms. Now, though he is playing with computer, he nearly losed and only his hero Pit Lord left. sevenzero is angry, he decided to cheat to turn defeat into victory. So he entered "whosyourdaddy", that let Pit Lord kill any hostile unit he damages immediately. As all Warcrafters know, Pit Lord masters a skill called Cleaving Attack and he can damage neighbour units of the unit he attacks. Pit Lord can choice a position to attack to avoid killing partial neighbour units sevenzero don't want to kill. Because sevenzero wants to win as soon as possible, he needs to know the minimum attack times to eliminate all the enemys.
 
Input
There are several cases. For each case, first line contains two integer N (2 ≤ N ≤ 55) and M (0 ≤ M ≤ N*N),and N is the number of hostile units. Hostile units are numbered from 1 to N. For the subsequent M lines, each line contains two integers A and B, that means A and B are neighbor. Each unit has no more than 4 neighbor units. The input is terminated by EOF.
 
Output
One line shows the minimum attack times for each case.
 
Sample Input
5 4
1 2
1 3
2 4
4 5
6 4
1 2
1 3
1 4
4 5
 
Sample Output
2
3

解析:裸的精确覆盖问题,不过要加优化,不过也是模板。

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int INF=1e9+;
const int ms=;
const int maxn=ms*ms;
int N,M,ans;
struct DLX
{
int n,id;
int L[maxn],R[maxn],U[maxn],D[maxn];
int C[maxn],S[maxn],loc[maxn][];
void init(int nn=) //传列长
{
n=nn;
for(int i=;i<=n;i++) U[i]=D[i]=i,L[i]=i-,R[i]=i+;
L[]=n; R[n]=;
id=n;
memset(S,,sizeof(S));
}
void AddRow(int x,int col,int A[]) //传入参数是行标号,列长,列数组
{
bool has=false;
int first=id+;
for(int y=;y<=col;y++)
{
if(A[y]==) continue;
has=true;
++id;
L[id]=id-; R[id]=id+;
D[id]=y; U[id]=U[y];
D[U[y]]=id; U[y]=id;
loc[id][]=x,loc[id][]=y;
C[id]=y; S[y]++;
}
if(!has) return;
R[id]=first; L[first]=id;
}
void Remove(int Size)
{
for(int j=D[Size];j!=Size;j=D[j])//将左右两边连接
L[R[j]]=L[j],R[L[j]]=R[j];
}
void Resume(int Size)
{
for(int j=U[Size];j!=Size;j=U[j])//恢复
L[R[j]]=R[L[j]]=j;
}
bool vis[ms];//标记行是否访问过
int h() //启发式函数
{
int ret=;
int i,j,k;
memset(vis,,sizeof(vis));
for(i=R[];i;i=R[i])
{
if(vis[i]) continue;
ret++;
for(j=D[i];j!=i;j=D[j]) //所有关联的标记了
for(k=R[j];k!=j;k=R[k]) vis[C[k]]=;
}
return ret;
}
void dfs(int step)
{
if(step+h()>=ans) return;
if(R[]==){ ans=min(ans,step); return; }
int Min=INF,c=-;
for(int i=R[];i;i=R[i]) if(Min>S[i]){ Min=S[i]; c=i; }
for(int i=D[c];i!=c;i=D[i])
{
Remove(i);
for(int j=R[i];j!=i;j=R[j]) Remove(j);
dfs(step+);
for(int j=L[i];j!=i;j=L[j]) Resume(j);
Resume(i);
}
return;
}
}dlx;
int Mart[ms][ms];
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
dlx.init(N);
ans=;
memset(Mart,,sizeof(Mart));
for(int i=;i<=N;i++) Mart[i][i]=;
while(M--)
{
int x,y;
scanf("%d%d",&x,&y);
Mart[x][y]=Mart[y][x]=;
}
for(int i=;i<=N;i++) dlx.AddRow(i,N,Mart[i]);
dlx.dfs();
printf("%d\n",ans);
}
return ;
}

代码

Hdu3498-whosyourdaddy(精确覆盖模板题)的更多相关文章

  1. hust 1017 dancing links 精确覆盖模板题

    最基础的dancing links的精确覆盖题目 #include <iostream> #include <cstring> #include <cstdio> ...

  2. HUST1017 Exact cover —— Dancing Links 精确覆盖 模板题

    题目链接:https://vjudge.net/problem/HUST-1017 1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 7673 次提交 3898 次 ...

  3. DLX精确覆盖与重复覆盖模板题

    hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...

  4. SPOJ 1771&&DLX精确覆盖,重复覆盖

    DLX的题,做过这题才算是会吧. 这道题转化成了精确覆盖模型来做,一开始,只是单纯的要覆盖完行列和斜线,WA. 后来醒悟了,不能这样,只要覆盖全部行或列即可.虽然如此,但某些细节地方很关键不能考虑到. ...

  5. HUST 1017 - Exact cover (Dancing Links 模板题)

    1017 - Exact cover 时间限制:15秒 内存限制:128兆 自定评测 5584 次提交 2975 次通过 题目描述 There is an N*M matrix with only 0 ...

  6. [DLX精确覆盖] hdu 1603 A Puzzling Problem

    题意: 给你n块碎片,这些碎片不能旋转.翻折. 问你能不能用当中的某些块拼出4*4的正方形. 思路: 精确覆盖裸题了 建图就是看看每一个碎片在4*4中能放哪些位置,这个就作为行. 列就是4*4=16个 ...

  7. POJ 3074 Sudoku DLX精确覆盖

    DLX精确覆盖.....模版题 Sudoku Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8336   Accepted: ...

  8. HDU 3111 Sudoku(精确覆盖)

    数独问题,输入谜题,输出解 既然都把重复覆盖的给写成模板了,就顺便把精确覆盖的模板也写好看点吧...赤裸裸的精确覆盖啊~~~水一水~~~然后继续去搞有点难度的题了... #include <cs ...

  9. ZOJ 3209 Treasure Map 精确覆盖

    题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...

随机推荐

  1. hdu4623:crime 数学优化dp

    鞍山热身赛的题,也是去年多校原题 题目大意: 求n个数的排列中满足相邻两个数互质的排列的数量并取模 当时的思路就是状压dp.. dp[i][state]  state用二进制记录某个数是否被取走,i ...

  2. poj3641:伪素数检测

    知道miller robin 素数测试中的伪素数定义就可以很容易解决,详见总结帖 #include <iostream> #include<stdio.h> #include& ...

  3. C++指针的操作和运算(转)

    既然指针是一种数据类型,那么它也应该有对应的操作或运算,正如整数能做加减乘除一样.但是每一种操作或运算都应该对这种数据类型有意义.比如两个实数可以用关系运算得知哪个大哪个小,而两个虚数却不能使用关系运 ...

  4. mysql c haracter

    基本概念 • 字符(Character)是指人类语言中最小的表义符号.例如’A'.’B'等:• 给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符,这一数值就是字符的编码(Encoding ...

  5. RequireJS入门(一)

    RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript.你将不再使用script标签在HTML中引入JS文件,以 ...

  6. [转]myeclipse 生成JAR包并引入第三方包

    myeclipse 生成JAR包并引入第三方包 我用的是myeclipse8.0 首先用myeclipse生成JAR 一.生成JAR包 1.点选项目右键—>Export 2.Java—>J ...

  7. iOS中的界面多选功能--(UICollectionView)

    文/Jacob_Pan(简书作者)原文链接:http://www.jianshu.com/p/9d28ebd0f5a2著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 最近做项目接触了一 ...

  8. PHP中$_SERVER的具体參数与说明

    PHP编程中常常须要用到一些server的一些资料.特把$_SERVER的具体參数整理下,方便以后使用. $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名称,与 document ...

  9. 完善GDAL与OpenCV间的数据格式转换与影像分块读写

    本博客为原创内容,未经博主允许禁止转载,商用,谢谢. 一.前言 关于GDAL与openCV间的数据格式转换,在我之前的博客中已有简要说明,这里,由于最近工作上经常用到openCV里的函数进行图像处理, ...

  10. 监控工具zabbix

    1 安装zabbixyum install -y epel-release安装rpm包的lamp环境 yum install  httpd mysql mysql-libs php php-mysql ...