• DLX模型问题


    问题: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<climits>
    #define N 60
    #define M 3600
    using namespace std;
    struct
    {
        int col,row;
    } node[M];
    int l[M],r[M],d[M],u[M],h[M],res[N],cntcol[N];
    int dcnt=-1,minn;
    int n,m;
    bool visit[N],mark[N][N];
    int H()
    {
        int count=0;
        bool hash[N];
        memset(hash,false,sizeof(hash));
        for(int i=r[0]; i!=0; i=r[i])
        {
            if(hash[i]) continue;
            //hash[i]=true;
            count++;
            for(int j=d[i]; j!=i; j=d[j])
                for(int k=r[j]; k!=j; k=r[k])
                    hash[node[k].col]=true;
        }
        return count;
    }
    void addnode(int &x)
    {
        ++x;
        r[x]=l[x]=u[x]=d[x]=x;
    }
    void insert_row(int rowx,int x)
    {
        r[l[rowx]]=x;
        l[x]=l[rowx];
        r[x]=rowx;
        l[rowx]=x;
    }
    void insert_col(int colx,int x)
    {
        d[u[colx]]=x;
        u[x]=u[colx];
        d[x]=colx;
        u[colx]=x;
    }
    void dlx_init(int cols)
    {
        memset(h,-1,sizeof(h));
        memset(cntcol,0,sizeof(cntcol));
        dcnt=-1;
        addnode(dcnt);
        for(int i=1; i<=cols; ++i)
        {
            addnode(dcnt);
            insert_row(0,dcnt);
        }
    }
    void insert_node(int x,int y)
    {
        //printf("insert %d %d ",x,y);
        cntcol[y]++;
        addnode(dcnt);
        node[dcnt].row=x;
        node[dcnt].col=y;
        insert_col(y,dcnt);
        if(h[x]==-1) h[x]=dcnt;
        else insert_row(h[x],dcnt);
    }
    void remove(int c)
    {
        for(int i=d[c]; i!=c; i=d[i])
        {
            l[r[i]]=l[i];
            r[l[i]]=r[i];
        }
    }
    void resume(int c)
    {
        for(int i=u[c]; i!=c; i=u[i])
        {
            l[r[i]]=i;
            r[l[i]]=i;
        }
    }
    void DLX(int deep)
    {
        if(deep+H()>=minn) return;
        if(r[0]==0)
        {
            if(minn>deep) minn=deep;
            return;
        }
        int min=INT_MAX,tempc;
        for(int i=r[0]; i!=0; i=r[i])
            if(cntcol[i]<min)
            {
                min=cntcol[i];
                tempc=i;
            }
        for(int i=d[tempc]; i!=tempc; i=d[i])
        {
            if(visit[node[i].row]) continue;
            res[deep]=node[i].row;
            remove(i);
            for(int j=r[i]; j!=i; j=r[j]) remove(j);
            DLX(deep+1);
            for(int j=l[i]; j!=i; j=l[j]) resume(j);
            resume(i);
        }
        return;
    }
    int main()
    {
        int k;
        int a,b;
        for(; ~scanf("%d%d",&n,&m);)
        {
            memset(mark,false,sizeof(mark));
            dlx_init(n);//初始化
            for(; m--;)
            {
                scanf("%d%d",&a,&b);
                mark[a][b]=mark[b][a]=true;
            }
            for(int i=1; i<=n; ++i)
                for(int j=1; j<=n; ++j)
                    if(mark[i][j]||i==j)
                        insert_node(i,j);
            minn=INT_MAX;
            DLX(0);
            printf("%d ",minn);
        }
        return 0;
    }

  • 相关阅读:
    cin 文件结束符
    C++ 代码折叠
    QTP/UFT 11.51 发布,支持Windows8和IE10等新特性
    QTP11.5测试手机 UFT Mobile
    Ranorex 4.0.2发布,支持Firefox19
    Selenium的WebDriver API 提交 W3C 标准化
    UFT/QTP11.5新特性
    2012年总结与2013年展望
    《TestComplete自动化测试实践》培训课程
    QTP11.5(HP UFT 11.5)下载地址
  • 原文地址:https://www.cnblogs.com/benchao/p/4591490.html
Copyright © 2020-2023  润新知