loj #6226. 「网络流 24 题」骑士共存问题
#6226. 「网络流 24 题」骑士共存问题
题目描述
在一个 n×n\text{n} \times \text{n}n×n 个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示。棋盘上某些方格设置了障碍,骑士不得进入。

对于给定的 n×n\text{n} \times \text{n}n×n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置多少个骑士,使得它们彼此互不攻击。
输入格式
第一行有两个正整数 n\text{n}n 和 m\text{m}m (1≤n≤200,0≤m≤n2−1)( 1 \leq n \leq 200, 0 \leq m \leq n^2 - 1 )(1≤n≤200,0≤m≤n2−1) 分别表示棋盘的大小和障碍数。
输出格式
输出计算出的共存骑士数。
样例
样例输入
3 2
1 1
3 3
样例输出
5
数据范围与提示
1≤n≤2001\leq n\leq 2001≤n≤200
0≤m≤n2−10 \leq m \leq n^2-10≤m≤n2−1
/*
加了当前弧优化和读入优化,快了不少
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 40010
#define INF 1000000000
int n,m,head[maxn],dis[maxn],num=,S,T,cur[maxn];
bool vis[maxn],mark[maxn];
struct node{int to,pre,v;}e[maxn*];
using namespace std;
int count(int x,int y){return n*(x-)+y;}
void Insert(int from,int to,int v){
e[++num].to=to;e[num].v=v;e[num].pre=head[from];head[from]=num;
e[++num].to=from;e[num].v=;e[num].pre=head[to];head[to]=num;
}
bool bfs(){
for(int i=S;i<=T;i++)dis[i]=-,cur[i]=head[i];
queue<int>q;
q.push(S);dis[S]=;
while(!q.empty()){
int now=q.front();q.pop();
for(int i=head[now];i;i=e[i].pre){
int to=e[i].to;
if(e[i].v>&&dis[to]==-){
dis[to]=dis[now]+;
if(to==T)return ;
q.push(to);
}
}
}
return dis[T]!=-;
}
int dinic(int x,int flow){
if(x==T||flow==){return flow;}
int rest=flow;
for(int &i=cur[x];i;i=e[i].pre){
int to=e[i].to;
if(dis[to]==dis[x]+&&e[i].v>){
int delta=dinic(to,min(rest,e[i].v));
e[i].v-=delta;
e[i^].v+=delta;
rest-=delta;
}
}
return flow-=rest;
}
bool check(int x,int y){
if(x<=n&&x>=&&y<=n&&y>=&&!mark[count(x,y)])return ;
return ;
}
int qread(){
int i=,j=;
char ch=getchar();
while(ch<''||ch>''){if(ch=='-')j=-;ch=getchar();}
while(ch<=''&&ch>=''){i=i*+ch-'';ch=getchar();}
return i*j;
}
int main(){
n=qread();m=qread();
S=,T=n*n+;
int x,y;
for(int i=;i<=m;i++){
x=qread();y=qread();
mark[count(x,y)]=;
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(mark[count(i,j)])continue;
if((i+j)&){
Insert(S,count(i,j),);
if(check(i-,j-))Insert(count(i,j),count(i-,j-),);
if(check(i-,j+))Insert(count(i,j),count(i-,j+),);
if(check(i+,j-))Insert(count(i,j),count(i+,j-),);
if(check(i+,j+))Insert(count(i,j),count(i+,j+),);
if(check(i-,j-))Insert(count(i,j),count(i-,j-),);
if(check(i-,j+))Insert(count(i,j),count(i-,j+),);
if(check(i+,j-))Insert(count(i,j),count(i+,j-),);
if(check(i+,j+))Insert(count(i,j),count(i+,j+),);
}
else Insert(count(i,j),T,);
}
}
int ans=;
while(bfs())ans+=dinic(S,INF);
printf("%d",n*n-ans-m);
}
loj #6226. 「网络流 24 题」骑士共存问题的更多相关文章
- 【刷题】LOJ 6226 「网络流 24 题」骑士共存问题
题目描述 在一个 \(\text{n} \times \text{n}\) 个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入. 对于给定的 \(\t ...
- 【刷题】LOJ 6227 「网络流 24 题」最长k可重线段集问题
题目描述 给定平面 \(\text{xoy}\) 上 \(n\) 个开线段组成的集合 \(\text{I}\) ,和一个正整数 \(k\) ,试设计一个算法. 从开线段集合 \(\text{I}\) ...
- [luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划
[luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划 试题描述 一个餐厅在相继的 \(N\) 天里,第 \(i\) 天需要 \(R_i\) 块餐巾 \((i=l,2,-,N)\) ...
- [LOJ#6002]「网络流 24 题」最小路径覆盖
[LOJ#6002]「网络流 24 题」最小路径覆盖 试题描述 给定有向图 G=(V,E).设 P 是 G 的一个简单路(顶点不相交)的集合.如果 V 中每个顶点恰好在 P 的一条路上,则称 P 是 ...
- loj #6014. 「网络流 24 题」最长 k 可重区间集
#6014. 「网络流 24 题」最长 k 可重区间集 题目描述 给定实直线 L LL 上 n nn 个开区间组成的集合 I II,和一个正整数 k kk,试设计一个算法,从开区间集合 I II 中选 ...
- loj #6013. 「网络流 24 题」负载平衡
#6013. 「网络流 24 题」负载平衡 题目描述 G 公司有 n nn 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n nn 个仓库的库存数量相同.搬运货物时 ...
- loj #6122. 「网络流 24 题」航空路线问题
#6122. 「网络流 24 题」航空路线问题 题目描述 给定一张航空图,图中顶点代表城市,边代表两个城市间的直通航线.现要求找出一条满足下述限制条件的且途经城市最多的旅行路线. 从最西端城市出发,单 ...
- loj #6121. 「网络流 24 题」孤岛营救问题
#6121. 「网络流 24 题」孤岛营救问题 题目描述 1944 年,特种兵麦克接到国防部的命令,要求立即赶赴太平洋上的一个孤岛,营救被敌军俘虏的大兵瑞恩.瑞恩被关押在一个迷宫里,迷宫地形复杂, ...
- [loj #6003]「网络流 24 题」魔术球 二分图最小路径覆盖,网络流
#6003. 「网络流 24 题」魔术球 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 ...
随机推荐
- python's eleventh day for me
python2 中没有nonlocal. 函数名是什么? 函数名就是函数的名字, 本质:变量,特殊的变量. 1.单独打印函数名: def func(): print(666) print(func) ...
- springboot成神之——log4j2的使用
本文介绍如何在spring-boot中使用log4j2 说明 依赖 日志记录语句 log4j2配置文件 本文介绍如何在spring-boot中使用log4j2 说明 log4j2本身使用是非常简单的, ...
- Python中常用模块一
random模块 import random # 应用数学计算 print(random.random()) # 取随机小数 范围是 0-1之间 # 应用抽奖 , 彩票 print(random.ra ...
- java将类和函数封装成jar
本来想用idea安装的,不过用maven生成后发现jar有20,30M肯定不对,后来还是用eclipse生成了,方便很多 环境: eclipse luna,jdk1.8_112 1.生成jar包,首先 ...
- JS中日期处理
- linux 压力测试工具 webbench
webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装: 1. wget htt ...
- XHProf安装使用笔记
编译安装 获取源代码包root@sourcjoy>wget http://pecl.php.net/get/xhprof-0.9.2.tgz解压root@sourcjoy>tar zxf ...
- mybatis内部类映射写法
直接上代码: mybatis内部类映射使用$而不是.
- java - Logback获取方法名称
java - Logback获取方法名称 摘自: https://blog.csdn.net/qq853632587/article/details/78222780 我们目前正在从 Log4J 迁移 ...
- ByteUnit
JDK里面有TimeUnit,看spark源码有个ByteUnit.这个类还是挺不错的. public enum ByteUnit { BYTE (1), KiB (1024L), MiB ((lon ...