网络流24题24. 骑士共存问题
来源:互联网 发布:c语言编程实例源代码 编辑:程序博客网 时间:2024/06/05 06:20
骑士共存问题
Description
在一个 n*n 个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示。棋盘上某些方格设置了障碍,骑士不得进入。
对于给定的 n*n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置多少个骑士,使得它们彼此互不攻击。
Input
第一行有 2 个正整数 n 和 m (1<=
Output
输出计算出的共存骑士数。
Sample Input
3 2
1 1
3 3
Sample Output
5
题解
首先把棋盘黑白染色,使相邻格子颜色不同。把所有可用的黑色格子看做二分图X集合中顶点,可用的白色格子看做Y集合顶点。建立附加源S汇T,从S向X集合中每个顶点连接一条容量为1的有向边,从Y集合中每个顶点向T连接一条容量为1的有向边。从每个可用的黑色格子向骑士一步能攻击到的可用的白色格子连接一条容量为无穷大的有向边。求出网络最大流,要求的结果就是可用格子的数量减去最大流量。
这题终于验证了当前弧优化的必要性!
在不加当前弧优化的时候Dinic算法最后两个点会超时,而优化后效果肉眼可见。(不加优化时时限
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;const int K = 200 + 10, N = 40000 + 10, M = 1000000 + 10, inf = 0x3f3f3f3f;const int dx[]={-2,-1,1,2,2,1,-1,-2}, dy[]={1,2,2,1,-1,-2,-2,-1};struct Edge{ int fr, to, cap, flow;}edg[M];int nxt[M], hd[N], tot;int s, t;int d[N], q[N], vis[N], dfn;int cur[N];int n, m;int mp[K][K];int ans;void insert(int u, int v, int w){ edg[tot].fr = u, edg[tot].to = v, edg[tot].cap = w; nxt[tot] = hd[u], hd[u] = tot; tot++; edg[tot].fr = v, edg[tot].to = u; nxt[tot] = hd[v], hd[v] = tot; tot++;}bool bfs(){ int head = 0, tail = 1; d[s] = 0; vis[s] = ++dfn; q[0] = s; while(head != tail){ int u = q[head++]; for(int i = hd[u]; i >= 0; i = nxt[i]){ Edge &e = edg[i]; if(vis[e.to] != dfn && e.cap > e.flow){ vis[e.to] = dfn; d[e.to] = d[u] + 1; q[tail++] = e.to; } } } return vis[t] == dfn;}int dfs(int x, int a){ if(x == t || a == 0) return a; int f, flow = 0; for(int &i = cur[x]; i >= 0; i = nxt[i]){ Edge &e = edg[i]; if(d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow)) > 0)){ flow += f; e.flow += f; edg[i^1].flow -= f; a -= f; if(a == 0) break; } } return flow;}void init(){ scanf("%d%d", &n, &m); int x, y; for(int i = 1; i <= m; i++){ scanf("%d%d", &x, &y); mp[x][y] = 1; }}int get(int x, int y){ return (x - 1) * n + y;}void build(){ memset(hd, -1, sizeof(hd)); s = 0, t = n * n + 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++){ if(mp[i][j]) continue; ans++; if(i + j & 1) insert(s, get(i, j), 1); else insert(get(i, j), t, 1); if(i + j & 1){ for(int k = 0; k <= 7; k++){ int x = i + dx[k], y = j + dy[k]; if(x > 0 && y > 0 && x <= n && y <= n && !mp[x][y]){ insert(get(i, j), get(x, y), inf); } } } } /*for(int i = 0; i < tot; i += 2) printf("%d %d %d\n", edg[i].fr, edg[i].to, edg[i].cap);*/}void work(){ build(); while(bfs()){ for(int i = s; i <= t; i++) cur[i] = hd[i]; ans -= dfs(s, inf); } printf("%d\n", ans);}int main(){ freopen("prog824.in", "r", stdin); freopen("prog824.out", "w", stdout); init(); work(); return 0;}
0 0
- 【网络流24题】骑士共存问题
- [网络流24题]骑士共存问题
- 网络流24题24. 骑士共存问题
- 骑士共存问题[网络流24题之24]
- 【线性规划与网络流24题 24】骑士共存问题
- 【codevs1922】[网络流24题]骑士共存问题
- cogs746 [网络流24题]骑士共存
- 线性规划与网络流24——骑士共存问题
- [网络流 24 题] 方格取数问题 骑士共存问题
- [网络流24题][CODEVS1922]骑士共存问题(最大流)
- [网络流24题][codevs1922] 骑士共存问题 二分图最大独立集
- [网络流24题]骑士共存问题 二分图/最大点权独立集
- cogs 746.[网络流24题] 骑士共存
- 线性规划与网络流24题の24 骑士共存问题 (二分图最大独立集)
- 【网络流24题】骑士共存问题(二分图染色+最大权独立子集+最小割)
- loj6226「网络流 24 题」骑士共存问题(二分图最大点独立集,最小割)
- 网络流二十四题之二十四 —— 骑士共存问题(KNI)
- 【网络流二十四题 骑士共存问题】【二分图点权最大独立集->最小割】
- 基本用法
- sql学习笔记
- 继承
- url_for
- Spark-1.3.1与Hive整合实现查询分析
- 网络流24题24. 骑士共存问题
- Vijos1359_Superprime
- Linux文件搜索命令
- Linux/Centos下安装配置花生壳
- 我对于创业公司的看法
- 软件测试在产品的各个阶段是做什么的?
- Vue开源项目库汇总
- c语言
- EasyUI常用控件禁用方法