线性规划与网络流24题の24 骑士共存问题 (二分图最大独立集)
来源:互联网 发布:c语言简单的图形编程 编辑:程序博客网 时间:2024/05/16 15:25
给一个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 | 1const 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
- 线性规划与网络流24题の24 骑士共存问题 (二分图最大独立集)
- [网络流24题][codevs1922] 骑士共存问题 二分图最大独立集
- [网络流24题]骑士共存问题 二分图/最大点权独立集
- [网络流24题] 24 骑士共存(二分图最大独立集,网络最小割)
- loj6226「网络流 24 题」骑士共存问题(二分图最大点独立集,最小割)
- 【线性规划与网络流24题 24】骑士共存问题
- 线性规划与网络流24——骑士共存问题
- 线性规划与网络流24题之方格取数问题 二分图点权最大独立集
- 【网络流二十四题 骑士共存问题】【二分图点权最大独立集->最小割】
- 【网络流24题】骑士共存问题(二分图染色+最大权独立子集+最小割)
- 骑士共存问题 二分图的最大独立集
- 线性规划与网络流24题の9 方格取数(二分图点权最大独立集)
- codevs 1922 骑士共存问题||二分图||最大独立集||二分图匹配||Dinic与匈牙利算法的讨论||网络流
- 【COGS】746 [网络流24题] 骑士共存 最大独立集
- 网络流24题之二十四 骑士共存 最大独立集
- Cogs 746. [网络流24题] 骑士共存(最大独立集)
- 【网络流24题】骑士共存问题
- [网络流24题]骑士共存问题
- OFBIZ 电子商城商品如何 进行多币种结算
- 关于nextLine()和next()的使用
- 制作Android 升级包
- C语言经典例题及答案1
- HDU 2874 —— Connections between cities(并查集+LCA)
- 线性规划与网络流24题の24 骑士共存问题 (二分图最大独立集)
- hdu 3579 (同余方程组,逐一合并大法)
- C语言经典例题及答案2
- Caught exception while loading file struts-default.xml 错误
- The password specified was incorrect. Please enter the correct password for the postgres windows use
- 转 OFBIZ 网站或店铺视觉主题(visual Theme)设计
- iOS APP 图标Icon和启动图片的大小、命名
- 转:全新安装Mac OSX 开发者环境 同时使用homebrew搭建 (LNMP开发环境)
- C语言经典例题及答案3