void-man

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

给出一个map,再给出几个星星的坐标,问用枪可以一次打一行或者一列的星星,问至少打几次可以把所有星星打完....

建图,最大匹配问题,左边是x横坐标的点,右边是y纵坐标,然后当某个星星在x,y 点上时候说明可以在x行或者y列打枪

因此把x-y连一条边,最终就是求最小的点覆盖所有的边(即和所有的边有关联)=最大匹配,直接匈牙利模版水过

#include <stdio.h>
#include <string.h>
#define N 505
int match[N],map[N][N];
int cn,mn;
bool used[N];
int find(int x)
{
    for(int i=1;i<=cn;i++)
    if(!used[i]&&map[x][i])
    {
        used[i]=true;
        if(match[i]==-1||find(match[i]))
        {
            match[i]=x;
            return true;
        }

    }
    return false;
}

int Hungry()
{
    int sum=0;
    for(int i=1;i<=cn;i++)
    {
        memset(used,false,sizeof(used));
        if(find(i))sum++;
    }
    return sum;
}

int main()
{
    int num,st,x,y;
    while(~scanf("%d%d",&cn,&mn))
    {
        memset(map,0,sizeof(map));
        memset(match,-1,sizeof(match));
        for(int i=1;i<=mn;i++)
        {
            scanf("%d%d",&x,&y);
            map[x][y]=1;
        }
        printf("%d\n",Hungry());

    }

}
posted on 2011-08-30 21:47  void-man  阅读(184)  评论(0)    收藏  举报