hdoj 5093 Battle ships 【二分图最大匹配】

题目:hdoj 5093 Battle ships


题意:给你一个n*m的图,图中有冰山 ‘# ’,浮冰 ‘o’ 以及普通海 ‘ * ’,如今要在海中布置尽可能多的炮弹。炮弹不能突波冰山,不能让炮弹互相攻击到。问最大能不知多少个?


分析:二分图的经典题目。关键在于怎么建图,图进行两次编号,按行编号,每一行中能攻击到的一块编号成相同的数,每一列相同。然后对行和列有编号的地方进行连边,求一次最大匹配就可以,我用最大流求的最大匹配。


AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = 1020;
const int inf = 0x3f3f3f3f;
int n,m;
int Min(int a,int b)
{
	return a>b?b:a;
}
struct Node
{
    int from,to,cap,flow;
};
vector<int> v[N];
vector<Node> e;
int vis[N];  //构建层次图
int cur[N];
void add_Node(int from,int to,int cap)
{
	Node tmp1,tmp2;
	tmp1.cap=from=from,tmp1.to=to,tmp1.cap=cap,tmp1.flow=0;
    e.push_back(tmp1);
	tmp2.from=to,tmp2.to=from,tmp2.cap=0,tmp2.flow=0;
    e.push_back(tmp2);
    int tmp=e.size();
    v[from].push_back(tmp-2);
    v[to].push_back(tmp-1);
}
bool bfs(int s,int t)
{
    Del(vis,-1);
    queue<int> q;
    q.push(s);
    vis[s] = 0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=0;i<v[x].size();i++)
        {
            Node tmp = e[v[x][i]];
            if(vis[tmp.to]<0 && tmp.cap>tmp.flow)  //第二个条件保证
            {
                vis[tmp.to]=vis[x]+1;
                q.push(tmp.to);
            }
        }
    }
    if(vis[t]>0)
        return true;
    return false;
}
int dfs(int o,int f,int t)
{
    if(o==t || f==0)  //优化
        return f;
    int a = 0,ans=0;
    for(int &i=cur[o];i<v[o].size();i++) //注意前面 ’&‘,非常重要的优化
    {
        Node &tmp = e[v[o][i]];
        if(vis[tmp.to]==(vis[o]+1) && (a = dfs(tmp.to,Min(f,tmp.cap-tmp.flow),t))>0)
        {
            tmp.flow+=a;
            e[v[o][i]^1].flow-=a; //存图方式
            ans+=a;
            f-=a;
            if(f==0)  //注意优化
                break;
        }
    }
    return ans;  //优化
}

int dinci(int s,int t)
{
    int ans=0;
    while(bfs(s,t))
    {
        Del(cur,0);
        int tm=dfs(s,inf,t);
        ans+=tm;
    }
    return ans;
}
void Clear(int x)
{
    for(int i=0;i<=x;i++)
        v[i].clear();
    e.clear();
}
int row[55][55],cal[55][55];
char mp[55][55];
bool ok[55*55*2];
int main()
{
    //freopen("Input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            getchar();
            for(int j=0;j<m;j++)
                scanf("%c",&mp[i][j]);
        }
        int s = 0,t = 1;
        int cnt = 2;
        memset(ok,false,sizeof(ok));
        memset(row,-1,sizeof(row));
        memset(cal,-1,sizeof(cal));
        for(int i=0;i<n;i++)  //给行编号
        {
            for(int j=0;j<m;j++)
            {
                if(mp[i][j]=='*'){
                    row[i][j] = cnt;
                    ok[cnt] = true;
                }
                else if(mp[i][j]=='#')
                {
                    if(ok[cnt]==true)
                        cnt++;
                }
            }
             if(ok[cnt]==true)
                cnt++;
        }
        for(int i=2;i<cnt;i++)
            add_Node(s,i,1);
        int ps = cnt;
        for(int j=0;j<m;j++)
        {
            for(int i=0;i<n;i++)
            {
                if(mp[i][j]=='*'){
                    cal[i][j] = cnt;
                    ok[cnt] = true;
                }
                else if(mp[i][j]=='#')
                {
                    if(ok[cnt]==true)
                        cnt++;
                }
            }
            if(ok[cnt]==true)
                cnt++;
        }
        for(int i=ps;i<cnt;i++)
            add_Node(i,t,1);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(row[i][j]>0 && cal[i][j]>0)
                {
                    add_Node(row[i][j],cal[i][j],1);
                }
            }
        }
        int ans = dinci(s,t);
        printf("%d
",ans);
        Clear(cnt);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/gcczhongduan/p/5254593.html