线性规划与网络流24题の24 骑士共存问题 (二分图最大独立集)

给一个N*N的棋盘,上面有M个障碍,问最多有多少个骑士不互相攻击(有障碍的格子上不能放骑士)。

将棋盘黑白染色,从源点向黑色格子加一条容量为1 的边,从白色格子向汇点连一条容量为1的边,从黑色格子向它可攻击的白色格子连容量为正无穷的边(黑色格子只能攻击白色格子)。然后用有效的格子总数 - 最大流就是答案。

论文:最小割模型在信息学竞赛中的应用

oj连接: http://wikioi.com/problem/1922/

//#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef double DB;
typedef long long ll;
typedef pair<int, int> PII;

#define pb push_back
#define MP make_pair
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const DB eps = 1e-6;
const int inf = ~0U>>1;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1000000007;
const int maxn = 40000 + 10;


///init是初始化要在加边之前初始化,然后调用max_flow(顶点数,边数,源点, 汇点)
const int maxv = 40000 + 10;///顶点个数
const int maxe = 1000000 + 10;///边数
struct node{
    int v, cap, next;
}edge[maxe];
int head[maxv], cnt;
int n;///n是节点个数,m是边数
int st, ed;///st是源点,ed是汇点
int gap[maxv], h[maxv];
void addedge(int u, int v, ll w){///有向图加边
    edge[cnt].v = v; edge[cnt].cap = w; edge[cnt].next = head[u]; head[u] = cnt++;///正向边
    edge[cnt].v = u; edge[cnt].cap = 0; edge[cnt].next = head[v]; head[v] = cnt++;///反向边
}
int dfs(int x, int cost){
    if(x == ed) return cost; ///当前节点是汇点,直接返回cost

    int can = cost, d, minh = n - 1;
    for(int i=head[x]; ~i; i=edge[i].next){
        int v = edge[i].v, w = edge[i].cap;
        if(w > 0){///如果这条边的容量大于0
            if(h[v] + 1 == h[x]){///如果这是允许弧
                if(can > w) d = w;///如果当前弧的容量小于之前可增广的容量
                else d = can;

                d = dfs(v, d);///从v开始可增广的容量为d

                ///更新弧的容量和可增广的容量
                edge[i].cap -= d;
                edge[i ^ 1].cap += d;
                can -= d;

                if(h[st] >= n) return cost - can;
                if(!can) break;///不能再继续增广
            }
            if(h[v] < minh) minh = h[v];///更新最小标号
        }
    }
    if(can == cost){///如果没有增广...GAP
        gap[h[x]]--;
        if(gap[h[x]] == 0) h[st] = n;///存在断层,没有增广路了
        h[x] = minh + 1;///重新标记,保证下次再访问的时候有流量
        gap[h[x]]++;
    }
    return cost - can;///在这个点之前可以增广的 - 访问这个点之后可以增广的 = 在这个点增广的容量
}
int max_flow(int N, int source, int sink){///SAP+GAP优化
    n = N;//m = M;
    st = source; ed = sink;
    memset(h, 0, sizeof(h));///h[i]表示i节点的标号
    memset(gap, 0, sizeof(gap));///gap[i]表示标号为i的节点个数
    gap[0] = n;///初始有n个节点标号为0
    int ret = 0;
    while(h[st] < n){
        ret += dfs(st, inf);
    }
    return ret;
}
void init(){
    memset(head, -1, sizeof(head)); cnt = 0;
}
bool vis[maxn];
void Dfs(int x){
    vis[x] = true;
    for(int i=head[x]; ~i; i=edge[i].next){
        if(edge[i].cap > 0 && !vis[edge[i].v]) Dfs(edge[i].v);
    }
}

int dxy[8][2] = {{2, 1}, {1, 2}, {1, -2}, {-2, 1}, {-1, -2}, {-2, -1}, {-1, 2}, {2, -1} };
int N, M;
bool g[222][222];
void add(int x, int y){
    for(int i=0; i<8; i++){
        int xx = x + dxy[i][0];
        int yy = y + dxy[i][1];
        if(xx > 0 && xx <= N && yy > 0 && yy <= N && !g[xx][yy])
            addedge((x - 1) * N + y, (xx - 1) * N + yy, inf);
    }
}
int main(){
    cin >> N >> M;
    init();
    memset(g, 0, sizeof(g));
    int x, y, sum = N * N - M;
    for(int i=1; i<=M; i++){
        cin >> x >> y;
        g[x][y] = 1;
    }
    for(int i=1; i<=N; i++)
    for(int j=1; j<=N; j++){
        int k = (i - 1) * N + j;
        if(g[i][j]) continue;///忽略放了障碍的点
        if((i + j) & 1) {add(i, j); addedge(0, k, 1);}
        else addedge(k, N * N + 1, 1);
    }
    sum -= max_flow(N * N + 2 - M, 0, N * N + 1);
    cout << sum << endl;
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值