hdu 3498(DLX 重复覆盖)

本文介绍了一个基于Warcraft背景的算法题目,玩家需利用特殊技能消灭敌方单位,通过最少次数的攻击达到目的。文章提供了完整的代码实现及解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单重复覆盖题。。。

 

whosyourdaddy

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1022    Accepted Submission(s): 502

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
 

 

Author
sevenzero
 

 

Source
 

 

Recommend
zhouzeyong
 
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 5000
#define INF 0x3ffffff

struct node
{
    int to,next;
}edge[N];

int R[N],L[N],U[N],D[N],num[N],col[N],line[N],H[N];
int head;
int n,m;
int g[60][60];
int nn,mm;
int cnt,pre[60];
int id;
int mi;

void add_edge(int x,int y)
{
    edge[cnt].to=y;
    edge[cnt].next=pre[x];
    pre[x]=cnt++;
}

void prepare()
{
    for(int i=0;i<=mm;i++)
    {
        num[i]=0;
        U[i]=i;
        D[i]=i;
        R[i]=i+1;
        L[i+1]=i;
    }
    R[mm]=0;
    L[0]=mm;
    memset(H,-1,sizeof(H));
}

void link(int tn,int tm)
{
    id++;
    num[line[id]=tm ]++;
    col[id]=tn;
    U[D[tm]]=id;
    D[id]=D[tm];
    U[id]=tm;
    D[tm]=id;
    if(H[tn]<0) H[tn]=R[id]=L[id]=id;
    else
    {

        L[R[H[tn]]]=id;
        R[id]=R[ H[tn] ];
        L[id]=H[tn];
        R[ H[tn] ]=id;
    }
}

void build()
{
    id=mm;
    prepare();
    for(int i=1;i<=n;i++)
    {
        link(i,i);
        for(int p=pre[i];p!=-1;p=edge[p].next)
        {
            int v=edge[p].to;
            link(i,v);
        }
    }    
}

int h()
{
    int mark[60];
    memset(mark,0,sizeof(mark));
    int sum=0;
    for(int i=R[head];i!=head;i=R[i])
    {
        if(mark[i]==0)
        {
            sum++;
            for(int j=D[i];j!=i;j=D[j])
                for(int k=R[j];k!=j;k=R[k])
                    mark[ line[k] ]=1;
        }
    }
    return sum;
}

void remove(int s)
{
    for(int i=D[s];i!=s;i=D[i])
    {
        L[R[i]]=L[i];
        R[L[i]]=R[i];
    }
}

void resume(int s)
{
    for(int i=D[s];i!=s;i=D[i])
        L[R[i]]=R[L[i]]=i;
}
void dfs(int s)
{
    if(s+h()>=mi) return ;
    if(R[head]==head)
    {
        mi=s;
        return ;
    }
    int tmi=INF,tu;
    for(int i=R[head];i!=head;i=R[i])
        if(num[i]<tmi)
        {
            tmi=num[i];
            tu=i;
        }
    for(int i=D[tu];i!=tu;i=D[i])
    {
        remove(i);
        for(int j=R[i];j!=i;j=R[j])
            remove(j);
        dfs(s+1);
        for(int j=L[i];j!=i;j=L[j])
            resume(j);
        resume(i);
    }
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        head=cnt=0;
        memset(pre,-1,sizeof(pre));
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add_edge(x,y);
            add_edge(y,x);
        }
        nn=0; mm=n;
        build();
        mi=INF;
        dfs(0);
        printf("%d\n",mi);
    }
}

 

转载于:https://www.cnblogs.com/chenhuan001/archive/2013/04/08/3007302.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值