[网络流24题][codevs1922] 骑士共存问题 二分图最大独立集
2017-05-19 11:15
465 查看
1922 骑士共存问题
题目描述 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
#include<iostream> #include<cstring> #include<cstdio> #define INF 0x7fffffff using namespace std; const int N = 405; int n,m,cnt=1,T,ans; int last[N*N],map ,num,h[N*N],q[N*N],nb ; int dx[8] = {1,1,2,2,-1,-1,-2,-2}; int dy[8] = {2,-2,1,-1,2,-2,1,-1}; struct Edge{ int to,v,next; }e[N*N*4]; void insert( int u, int v, int w ){ e[++cnt].to = v; e[cnt].v = w; e[cnt].next = last[u]; last[u] = cnt; e[++cnt].to = u; e[cnt].v = 0; e[cnt].next = last[v]; last[v] = cnt; } bool bfs(){ int head = 0, tail = 1; memset(h,-1,sizeof(h)); q[0] = 0; h[0] = 0; while( head != tail ){ int now = q[head++]; if( now == T ) return true; for( int i = last[now]; i; i = e[i].next ) if( e[i].v && h[e[i].to] == -1 ){ h[e[i].to] = h[now] + 1; q[tail++] = e[i].to; } } return h[T] != -1; } int dfs( int x, int f ){ if( x == T ) return f; int w,used = 0; for( int i = last[x]; i; i = e[i].next ) if( e[i].v && h[e[i].to] == h[x] + 1 ){ w = dfs( e[i].to, min(f-used,e[i].v) ); e[i].v -= w; e[i^1].v += w; used += w; if( used == f ) return used; } if( !used ) h[x] = -1; return used; } void dinic(){ while(bfs()){ ans += dfs(0,INF); } } void build(){ T = n*n+1; for( int i = 1; i <= n; i++ ) for( int j = 1; j <= n; j++ ) nb[i][j] = ++num; for( int i = 1; i <= n; i++ ) for( int j = 1; j <= n; j++ ) if( !map[i][j] ){ if( (i+j) % 2 == 0 ){ insert(0,nb[i][j],1); for( int k = 0; k < 8; k++ ){ int xx = i + dx[k]; int yy = j + dy[k]; if( xx < 1 || xx > n || yy < 1 || yy > n || map[xx][yy] ) continue; insert( nb[i][j], nb[xx][yy], 1 ); } } else{ insert( nb[i][j], T, 1 ); } } } int main(){ scanf("%d%d", &n, &m); for( int i = 1, x, y; i <= m; i++ ) scanf("%d%d", &x, &y ), map[x][y] = 1; build(); dinic(); printf("%d",n*n-m-ans); return 0; }
相关文章推荐
- codevs 1922 骑士共存问题||二分图||最大独立集||二分图匹配||Dinic与匈牙利算法的讨论||网络流
- 【Codevs1922】骑士共存问题(最小割,二分图最大独立集转最大匹配)
- [网络流24题][CODEVS1922]骑士共存问题(最大流)
- 【codevs1922】[网络流24题]骑士共存问题
- loj6226「网络流 24 题」骑士共存问题(二分图最大点独立集,最小割)
- 线性规划与网络流24题の24 骑士共存问题 (二分图最大独立集)
- 【网络流24题】骑士共存问题(二分图染色+最大权独立子集+最小割)
- [CODEVS1922]骑士共存问题(最大流)
- 【codevs1922】骑士共存问题——网络流
- [网络流24题]骑士共存问题 二分图/最大点权独立集
- codevs 1922 骑士共存问题 网络流
- [网络流24题] 24 骑士共存(二分图最大独立集,网络最小割)
- codevs1922(二分图最大独立集)
- 【COGS】746 [网络流24题] 骑士共存 最大独立集
- [网络流24题][CODEVS1904]最小路径覆盖问题(最大流||匈牙利算法)
- LOJ6226 「网络流 24 题 - 22」 骑士共存 二分图坠大点独立集
- Cogs 746. [网络流24题] 骑士共存(最大独立集)
- 骑士共存问题 二分图的最大独立集
- [luoguP3355] 骑士共存问题(二分图最大独立集)
- wikioi 1922 骑士共存问题 最大独立集