Codevs_P1922 骑士共存问题(Dinic算法最大流+二分图匹配)

48 篇文章 0 订阅
23 篇文章 0 订阅

时间限制: 2 s
空间限制: 256000 KB
题目等级 : 大师 Master
题目描述 Description
在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示。棋盘
上某些方格设置了障碍,骑士不得进入。

对于给定的n*n个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置多少个骑
士,使得它们彼此互不攻击。

输入描述 Input Description
第一行有2 个正整数n 和m (1<=n<=200, 0<=m< n^2),
分别表示棋盘的大小和障碍数。接下来的m 行给出障碍的位置。每行2 个正整数,表示障
碍的方格坐标。

输出描述 Output Description
将计算出的共存骑士数输出

样例输入 Sample Input
3 2
1 1
3 3

样例输出 Sample Output
5

友情提示:骑士与四周距离为“日” 型方格的棋不能共存

还是老思路,黑白染色,黑格与未被阻碍白格ri型相连,流量INF,超级源点与黑格相连流量为1,未被阻碍的白个与超级汇点相连,流量为1
Dinic算法:bfs将图分层,保证每次都是最短路,dfs求阻塞流;
ps:Dinic好快qwq,普通最大流SPFA会超时,第一个是Dinic的递归算法,第二个是非递归算法

#include<cstdio>
#include<climits>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define N 205
#define INF INT_MAX/3*2
struct Min_Cut{
    struct Edge{
        int fr,to,cap,flow;
        Edge(int a,int b,int c,int d):fr(a),to(b),cap(c),flow(d){}
    };
    vector<Edge> edge;vector<int> g[N*N];
    int a[N*N],p[N*N],d[N*N],cur[N*N];bool b[N*N];
    int t,n,m,k,S,T,flow,ans;

    int hash(int i,int j){return (i-1)*n+j;}
    void in(int &x){
        x=0;char ch=getchar();
        while(ch>'9'||ch<'0') ch=getchar();
        while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return;
    }

    void Add_Edge(int fr,int to,int cap,int flow){
        edge.push_back(Edge(fr,to,cap,flow));
        edge.push_back(Edge(to,fr,0,0));
        t=edge.size();
        g[fr].push_back(t-2);g[to].push_back(t-1);return;
    }

    void init(){
        in(n),in(m);int x,y;
        S=0,T=n*n+1;ans=n*n-m;
        while(m--){
            in(x),in(y);
            b[hash(x,y)]=true;
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                if(!b[hash(i,j)]&&((i+j)&1)){
                    Add_Edge(S,hash(i,j),1,0);
                    if(i-2>0&&j-1>0&&!b[hash(i-2,j-1)]) Add_Edge(hash(i,j),hash(i-2,j-1),INF,0);
                    if(i-2>0&&j+1<=n&&!b[hash(i-2,j+1)])Add_Edge(hash(i,j),hash(i-2,j+1),INF,0);
                    if(i-1>0&&j-2>0&&!b[hash(i-1,j-2)]) Add_Edge(hash(i,j),hash(i-1,j-2),INF,0);
                    if(i-1>0&&j+2<=n&&!b[hash(i-1,j+2)])Add_Edge(hash(i,j),hash(i-1,j+2),INF,0);

                    if(i+2<=n&&j-1>0&&!b[hash(i+2,j-1)]) Add_Edge(hash(i,j),hash(i+2,j-1),INF,0);
                    if(i+2<=n&&j+1<=n&&!b[hash(i+2,j+1)])Add_Edge(hash(i,j),hash(i+2,j+1),INF,0);
                    if(i+1<=n&&j-2>0&&!b[hash(i+1,j-2)]) Add_Edge(hash(i,j),hash(i+1,j-2),INF,0);
                    if(i+1<=n&&j+2<=n&&!b[hash(i+1,j+2)])Add_Edge(hash(i,j),hash(i+1,j+2),INF,0);
                }
                if(!b[hash(i,j)]&&!((i+j)&1))   Add_Edge(hash(i,j),T,1,0);
            }

    }

    bool bfs(){
        memset(d,-1,sizeof(d));memset(b,0,sizeof(b));
        queue<int> q;int x;d[S]=0;b[S]=true;q.push(S);
        while(!q.empty()){
            x=q.front();q.pop();
            for(int i=0;i<g[x].size();i++){
                Edge &e=edge[g[x][i]];
                if(!b[e.to]&&e.cap>e.flow){
                    b[e.to]=true;d[e.to]=d[x]+1;q.push(e.to);
                }
            }
        }
        return b[T];
    }

    int dfs(int x,int a){
        if(x==T||a==0) return a;
        int flow=0,f;
        for(int &i=cur[x];i<g[x].size();i++){
            Edge &e=edge[g[x][i]];
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;edge[g[x][i]^1].flow-=f;
                flow+=f;a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }

    void Max_Flow(){
        flow=0;
        while(bfs()){
            memset(cur,0,sizeof(cur));
            flow+=dfs(S,INF);
        }
    /*  for(;;){
            memset(a,0,sizeof(a));a[S]=INF;int x;
            queue<int> q;q.push(S);
            while(!q.empty()){
                x=q.front();q.pop();
                for(int i=0;i<g[x].size();i++){
                    Edge &e=edge[g[x][i]];
                    if(!a[e.to]&&e.cap>e.flow){
                        a[e.to]=min(e.cap-e.flow,a[x]);
                        p[e.to]=g[x][i];
                        q.push(e.to);
                    }
                }
                if(a[T]) break;
            }
            if(!a[T]) break;
            flow+=a[T];
            for(x=T;x!=S;x=edge[p[x]].fr){
                edge[p[x]].flow+=a[T];edge[p[x]^1].flow-=a[T];
            }
        }*/
    }

    void solve(){
        init();Max_Flow();printf("%d\n",ans-flow);
    }

}s;
int main(){
    s.solve();return 0;
}

这是Dinic的非递归算法优化

#include<cstdio>
#include<climits>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define N 205
#define INF INT_MAX/3*2
struct Min_Cut{
    struct Edge{
        int fr,to,cap,flow;
        Edge(int a,int b,int c,int d):fr(a),to(b),cap(c),flow(d){}
    };
    vector<Edge> edge;vector<int> g[N*N];
    int a[N*N],p[N*N],d[N*N],cur[N*N];bool b[N*N];
    int t,n,m,S,T,flow,ans;

    int hash(int i,int j){return (i-1)*n+j;}
    void in(int &x){
        x=0;char ch=getchar();
        while(ch>'9'||ch<'0') ch=getchar();
        while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return;
    }

    void Add_Edge(int fr,int to,int cap,int flow){
        edge.push_back(Edge(fr,to,cap,flow));
        edge.push_back(Edge(to,fr,0,0));
        t=edge.size();
        g[fr].push_back(t-2);g[to].push_back(t-1);return;
    }

    void init(){
        in(n),in(m);int x,y;
        S=0,T=n*n+1;ans=n*n-m;
        while(m--){
            in(x),in(y);
            b[hash(x,y)]=true;
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                if(!b[hash(i,j)]&&((i+j)&1)){
                    Add_Edge(S,hash(i,j),1,0);
                    if(i-2>0&&j-1>0&&!b[hash(i-2,j-1)]) Add_Edge(hash(i,j),hash(i-2,j-1),INF,0);
                    if(i-2>0&&j+1<=n&&!b[hash(i-2,j+1)])Add_Edge(hash(i,j),hash(i-2,j+1),INF,0);
                    if(i-1>0&&j-2>0&&!b[hash(i-1,j-2)]) Add_Edge(hash(i,j),hash(i-1,j-2),INF,0);
                    if(i-1>0&&j+2<=n&&!b[hash(i-1,j+2)])Add_Edge(hash(i,j),hash(i-1,j+2),INF,0);

                    if(i+2<=n&&j-1>0&&!b[hash(i+2,j-1)]) Add_Edge(hash(i,j),hash(i+2,j-1),INF,0);
                    if(i+2<=n&&j+1<=n&&!b[hash(i+2,j+1)])Add_Edge(hash(i,j),hash(i+2,j+1),INF,0);
                    if(i+1<=n&&j-2>0&&!b[hash(i+1,j-2)]) Add_Edge(hash(i,j),hash(i+1,j-2),INF,0);
                    if(i+1<=n&&j+2<=n&&!b[hash(i+1,j+2)])Add_Edge(hash(i,j),hash(i+1,j+2),INF,0);
                }
                if(!b[hash(i,j)]&&!((i+j)&1))   Add_Edge(hash(i,j),T,1,0);
            }

    }

    bool bfs(){
        memset(d,-1,sizeof(d));memset(b,0,sizeof(b));
        queue<int> q;int x;d[S]=0;b[S]=true;q.push(S);
        while(!q.empty()){
            x=q.front();q.pop();
            for(int i=0;i<g[x].size();i++){
                Edge &e=edge[g[x][i]];
                if(!b[e.to]&&e.cap>e.flow){
                    b[e.to]=true;d[e.to]=d[x]+1;q.push(e.to);
                }
            }
        }
        return b[T];
    }

/*  int dfs(int x,int a){
        if(x==T||a==0) return a;
        int flow=0,f;
        for(int &i=cur[x];i<g[x].size();i++){
            Edge &e=edge[g[x][i]];
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;edge[g[x][i]^1].flow-=f;
                flow+=f;a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }*/

    void Max_Flow(){
        flow=0;
        while(bfs()){
            int k=0,x=S;
            memset(cur,0,sizeof(cur));
            for(;;){
                if(x==T){
                    int mine=-1,minf=INF;
                    for(int i=0;i<k;i++)
                        if(edge[p[i]].cap-edge[p[i]].flow<minf){
                            minf=edge[p[i]].cap-edge[p[i]].flow;
                            mine=i;
                        }
                    for(int i=0;i<k;i++){
                        edge[p[i]].flow+=minf;
                        edge[p[i]^1].flow-=minf;
                    }
                    k=mine;x=edge[p[mine]].fr;flow+=minf;
                }
                for(int &i=cur[x];i<g[x].size();i++){
                    Edge &e=edge[g[x][i]];
                    if(e.cap>e.flow&&d[x]+1==d[e.to]) break;
                }
                if(cur[x]<g[x].size()){
                    p[k++]=g[x][cur[x]];
                    x=edge[g[x][cur[x]]].to;
                }
                else{
                    if(k==0) break;
                    d[x]=-1;k--;x=edge[p[k]].fr;
                }
            }
        }
    /*  for(;;){
            memset(a,0,sizeof(a));a[S]=INF;int x;
            queue<int> q;q.push(S);
            while(!q.empty()){
                x=q.front();q.pop();
                for(int i=0;i<g[x].size();i++){
                    Edge &e=edge[g[x][i]];
                    if(!a[e.to]&&e.cap>e.flow){
                        a[e.to]=min(e.cap-e.flow,a[x]);
                        p[e.to]=g[x][i];
                        q.push(e.to);
                    }
                }
                if(a[T]) break;
            }
            if(!a[T]) break;
            flow+=a[T];
            for(x=T;x!=S;x=edge[p[x]].fr){
                edge[p[x]].flow+=a[T];edge[p[x]^1].flow-=a[T];
            }
        }*/
    }

    void solve(){
        init();Max_Flow();printf("%d\n",ans-flow);
    }

}s;
int main(){
    s.solve();return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值