您的位置:首页 > 其它

hdu 3498 whosyourdaddy(重复覆盖+估价函数剪枝)

2014-10-04 22:52 369 查看

whosyourdaddy

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1401 Accepted Submission(s): 699



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
One line shows the minimum attack times for each case.

[align=left]Sample Input[/align]

5 4
1 2
1 3
2 4
4 5
6 4
1 2
1 3
1 4
4 5


[align=left]Sample Output[/align]

2
3

题意:有n个敌人,每个敌人有若干个敌人与其相邻,攻击一个敌人时能消灭该敌人和与之相邻的敌人,问最少攻击多少次能把敌人都消灭。思路:DLX重复覆盖,需要用估价函数剪枝。
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <bitset>
#include <queue>
#define ll long long
using namespace std;

const int maxn = 10005;
const int INF = 1e9;

int Min, n, m;
struct DLX{
#define FF(i,A,s) for(int i = A[s];i != s;i = A[i])
int L[maxn],R[maxn],U[maxn],D[maxn];
int size,col[maxn],row[maxn],s[maxn],H[maxn];
bool vis[60];
int ans[maxn],cnt;
void init(int m){
for(int i=0;i<=m;i++){
L[i]=i-1;R[i]=i+1;U[i]=D[i]=i;s[i]=0;
}
memset(H,-1,sizeof(H));
L[0]=m;R[m]=0;size=m+1;
}
void link(int r,int c){
U[size]=c;D[size]=D[c];U[D[c]]=size;D[c]=size;
if(H[r]<0)H[r]=L[size]=R[size]=size;
else {
L[size]=H[r];R[size]=R[H[r]];
L[R[H[r]]]=size;R[H[r]]=size;
}
s[c]++;col[size]=c;row[size]=r;size++;
}
void del(int c){//精确覆盖
L[R[c]]=L[c];R[L[c]]=R[c];
FF(i,D,c)FF(j,R,i)U[D[j]]=U[j],D[U[j]]=D[j],--s[col[j]];
}
void add(int c){  //精确覆盖
R[L[c]]=L[R[c]]=c;
FF(i,U,c)FF(j,L,i)++s[col[U[D[j]]=D[U[j]]=j]];
}
bool dfs(int k){//精确覆盖
if(!R[0]){
cnt=k;return 1;
}
int c=R[0];FF(i,R,0)if(s[c]>s[i])c=i;
del(c);
FF(i,D,c){
FF(j,R,i)del(col[j]);
ans[k]=row[i];if(dfs(k+1))return true;
FF(j,L,i)add(col[j]);
}
add(c);
return 0;
}
void remove(int c){//重复覆盖
FF(i,D,c)L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c){//重复覆盖
FF(i,U,c)L[R[i]]=R[L[i]]=i;
}
int A(){//估价函数
int res=0;
memset(vis,0,sizeof(vis));
FF(i,R,0)if(!vis[i]){
res++;vis[i]=1;
FF(j,D,i)FF(k,R,j)vis[col[k]]=1;
}
return res;
}
void dance(int now){//重复覆盖
if(now + A() >= Min) return;
if(R[0]==0){
if(now < Min) Min = now;
return;
}
int temp=INF,c;
FF(i,R,0)if(temp>s[i])temp=s[i],c=i;
FF(i,D,c){
remove(i);
FF(j,R,i)remove(j);
dance(now+1);
FF(j,L,i)resume(j);
resume(i);
}
}
}dlx;
bool G[100][100];
int main()
{
int a, b;
while(~scanf("%d%d", &n, &m))
{
dlx.init(n);
memset(G, 0, sizeof(G));
for(int i = 0; i < m; i++)
{
scanf("%d%d", &a, &b);
G[a][b] = G[b][a] = 1;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(G[i][j] || i == j) dlx.link(i, j);
Min = n;
dlx.dance(0);
printf("%d\n", Min);
}
return 0;
}


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