P2825 [HEOI2016/TJOI2016] 游戏

题意

一个 \(n\times n\) 的矩阵,每个点是空地 *,软石头 x 和硬石头 # 中的一种。现在要在空地上放置炸弹,一个炸弹能攻击从祂出发向上下左右四个方向走,直到碰到边界或硬石头位置经过的所有格子。要求两个炸弹不能互相攻击,问最多能放置多少个炸弹。
\(n\le50\)

思路

二分图匹配经典题目。
先横向遍历矩阵,把同一行内由软石头和空地组成的极长连续段表为同一个号,把这些标号看作 \(X\) 部。
再纵向遍历矩阵,把同一列内由软石头和空地组成的极长连续段表为同一个号,把这些标号看作 \(Y\) 部。
对于一个空地,祂所在的行标号和列标号连边。跑二分图匹配,输出最大匹配。

代码

用的是网络流。

// Problem: P2825 [HEOI2016/TJOI2016] 游戏
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2825
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
namespace IO{
    template<typename T>
    inline void read(T&x){
        x=0;char c=getchar();bool f=0;
        while(!isdigit(c)) c=='-'?f=1:0,c=getchar();
        while(isdigit(c)) x=x*10+c-'0',c=getchar();
        f?x=-x:0;
    }
    template<typename T>
    inline void write(T x){
        if(x==0){putchar('0');return ;}
        x<0?x=-x,putchar('-'):0;short st[50],top=0;
        while(x) st[++top]=x%10,x/=10;
        while(top) putchar(st[top--]+'0');
    }
    inline void read(char&c){c=getchar();while(isspace(c)) c=getchar();}
    inline void write(char c){putchar(c);}
    inline void read(string&s){s.clear();char c;read(c);while(!isspace(c)&&~c) s+=c,c=getchar();}
    inline void write(string s){for(int i=0,len=s.size();i<len;i++) putchar(s[i]);}
    template<typename T>inline void write(T*x){while(*x) putchar(*(x++));}
    template<typename T,typename...T2> inline void read(T&x,T2&...y){read(x),read(y...);}
    template<typename T,typename...T2> inline void write(const T x,const T2...y){write(x),putchar(' '),write(y...),sizeof...(y)==1?putchar('\n'):0;}
}using namespace IO;
const int maxn=60,inf=1000000000;
int n,m,bhh[maxn][maxn],bhl[maxn][maxn];
char a[maxn][maxn];
template<int maxn,int maxm>class LSQXX{
public:
    int head[maxn],nxt[maxm*2],to[maxm*2],val[maxn*2],cnt=1;
    void add(int u,int v,int w){nxt[++cnt]=head[u],to[cnt]=v,val[cnt]=w,head[u]=cnt;}
    void clear(){cnt=0;memset(head,0,sizeof(head));}
};
class Network_Flow{
private:
    LSQXX<maxn*maxn*2,maxn*maxn*3>e;
    int cur[maxn*maxn*2],d[maxn*maxn*2],s,t;
    void edge_add(int u,int v,int w){e.add(u,v,w),e.add(v,u,0);}
    bool bfs(){
        for(int i=s;i<=t;i++) d[i]=inf;
        d[s]=0;queue<int>q;q.push(s);
        while(!q.empty()){
            int u=q.front();q.pop();
            for(int i=e.head[u];i;i=e.nxt[i]){
                int v=e.to[i];
                if(!e.val[i]) continue;
                if(d[v]>d[u]+1) d[v]=d[u]+1,q.push(v);
            }
        }
        return d[t]!=inf;
    }
    int dfs(int u,int flow=inf){
        if(u==t||flow==0) return flow;
        int ans=0;
        for(int&i=cur[u];i;i=e.nxt[i]){
            int v=e.to[i];
            if(d[v]!=d[u]+1) continue;
            int nwflow=dfs(v,min(flow,e.val[i]));
            e.val[i]-=nwflow,e.val[i^1]+=nwflow;
            flow-=nwflow,ans+=nwflow;
            if(flow==0) break;
        }
        return ans;
    }
public:
    void build(){
        s=t=0;
        for(int i=1;i<=n;i++){
            t++;
            for(int j=1;j<=m;j++){
                if(a[i][j]=='#') t++;
                bhh[i][j]=t;
            }
        }
        for(int j=1;j<=m;j++){
            t++;
            for(int i=1;i<=n;i++){
                if(a[i][j]=='#') t++;
                bhl[i][j]=t;
            }
        }
        t++;
        for(int i=1;i<=bhh[n][m];i++) edge_add(s,i,1);
        for(int i=bhh[n][m]+1;i<=bhl[n][m];i++) edge_add(i,t,1);
        for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){
            if(a[i][j]!='*') continue;
            edge_add(bhh[i][j],bhl[i][j],1);
        }
    }
    int work(){
        int ans=0;
        while(bfs()){
            for(int i=s;i<=t;i++) cur[i]=e.head[i];
            ans+=dfs(s);
        }
        return ans;
    }
}wll;
signed main(){
    read(n,m);
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) read(a[i][j]);
    wll.build();
    write(wll.work());
    return 0;
}
posted @ 2026-05-11 15:30  Link-Cut_Trees  阅读(12)  评论(0)    收藏  举报