现在的位置: 首页 > 综合 > 正文

hdu 3498 whosyourdaddy 重复覆盖+DLX 每次攻击i和i的所有邻居,最少攻击多少人才能杀死所有敌人

2013年10月08日 ⁄ 综合 ⁄ 共 2769字 ⁄ 字号 评论关闭

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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define eps 1e-8
#define N 550
#define V 360000
int n,m;//n行 m列
int L[V],R[V];
int D[V],U[V];
int C[V];
int S[N],H[N];
int ak,size;//ak 最少多少行可以覆盖所有列(可重复)
void Link(int r,int c)
{
    S[c]++;C[size]=c;
    U[size]=U[c];D[U[c]]=size;
    D[size]=c;U[c]=size;
    if(H[r]==-1) H[r]=L[size]=R[size]=size;
    else
    {
        L[size]=L[H[r]];R[L[H[r]]]=size;
        R[size]=H[r];L[H[r]]=size;
    }
    size++;
}
void remove(int c)
{
    int i;
    for(i=D[c];i!=c;i=D[i])
        L[R[i]]=L[i],R[L[i]]=R[i];
}
void resume(int c)
{
    int i;
    for(i=U[c];i!=c;i=U[i])
        L[R[i]]=R[L[i]]=i;
}
int h()
{
    int i,j,k,count=0;
    bool visit[N];
    memset(visit,0,sizeof(visit));
    for(i=R[0];i;i=R[i])
    {
        if(visit[i]) continue;
        count++;
        visit[i]=1;
        for(j=D[i];j!=i;j=D[j])
        {
            for(k=R[j];k!=j;k=R[k])
                visit[C[k]]=1;
        }
    }
    return count;
}
void Dance(int k)
{
    int i,j,c,Min,ans;
    ans=h();
    if(k+ans>=ak) return;
    if(!R[0])
    {
        if(k<ak) ak=k;
        return;
    }
    for(Min=N,i=R[0];i;i=R[i])
        if(S[i]<Min) Min=S[i],c=i;
    for(i=D[c];i!=c;i=D[i])
    {
        remove(i);
        for(j=R[i];j!=i;j=R[j])
            remove(j);
        Dance(k+1);
        for(j=L[i];j!=i;j=L[j])
            resume(j);
        resume(i);
    }
    return;
}

int mat[300][300];
int main()
{
    int tn,tm;
    while(scanf("%d%d",&tn,&tm)==2)
    {
        //每个人当作一列,每个人当作一行 每次覆盖自己和他邻居
        n=m=tn;
        memset(mat,0,sizeof(mat));
        while(tm--)
        {
            int u,v;scanf("%d%d",&u,&v);
            mat[u][v]=mat[v][u]=1;
        }
        for(int i=0;i<=m;i++)
        {
            S[i]=0;
            U[i]=D[i]=i;
            L[i+1]=i;R[i]=i+1;
        }R[m]=0;
        memset(H,-1,sizeof(H));
        size=m+1;
        for(int i=1;i<=n;i++)
        {
            mat[i][i]=1;//important
            for(int j=1;j<=m;j++)
            {
                if(mat[i][j]) Link(i,j);
            }
        }
        ak=N;
        Dance(0);
        printf("%d\n",ak);
    }
    return 0;
}

抱歉!评论已关闭.