UVA 11419 SAM I AM 二分图+最小覆盖点

我不想给题目

题意:

给你一张二维图,上面有石头,然后你就用你TN的意大利炮去轰这些石头吧(意大利炮的威力很大,所以能够直线AOE),问你最少打几炮(只能横着或者竖着打)?输出打炮的位置。

解题思路:

题目等价于最多有多少个石头既不在同一行也不再同一列上。有石头的地方行和列就能匹配,找出最大匹配即可(匈牙利模板 )。

然后是寻找打炮位置,看代码吧。。

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<vector>
using namespace std;
const int maxn=2*1005;
const int maxm=2*1005*1005;
const int INF=0x3f3f3f3f;
int fir[maxn];
int u[maxm],v[maxm],nex[maxm];
int e_max;
int match[maxn];
bool check[maxn];
int g[maxn][maxn];
int ans;
inline void init()
{
         memset(fir, -1, sizeof fir);
         e_max = 0;
}
inline void add_edge(int s, int t)
 {
        int e = e_max++;
        u[e] = s;
        v[e] = t;
        nex[e] = fir[u[e]];
        fir[u[e]] = e;
}
int n,m,e;
bool dfs(int u)
{
        check[u]=1;
        for(int e=fir[u];~e;e=nex[e])
        {
                int vv=v[e];
                if(!check[vv])
                {
                        check[vv]=1;
                        if(match[vv]==-1||dfs(match[vv]))
                        {
                                match[vv]=u;
                                match[u]=vv;
                                return 1;
                        }
                }
        }
        return 0;
}
int  hungary()
{
        ans=0;
        memset(match,-1,sizeof match);
        for(int i=1;i<=n;i++)
        {
                if(match[i]==-1)
                {
                        memset(check,0,sizeof check);
                        if(dfs(i))
                              ans++;
                }
        }
}
int main()
{
        int a,b;
        while(scanf("%d%d%d",&n,&m,&e)!=EOF&&(m+n+e))
        {
                init();
                memset(g,0,sizeof g);
                for(int i=0;i<e;i++)
                {
                        scanf("%d%d",&a,&b);
                        add_edge(a,b+n);
                }
                hungary();
                printf("%d",ans);
                memset(check,0,sizeof check);
                for(int i=1;i<=n;i++)
                {
                        if(match[i]==-1)
                                dfs(i);
                }
                for(int i=1;i<=n;i++)
                        if(!check[i])
                                printf(" r%d",i);
                for(int i=n+1;i<=m+n;i++)
                        if(check[i])
                                printf(" c%d",i-n);
                printf("\n");
        }
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值