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 上传者: 匿名 提交提交记录统计讨论测试数据 ...
 
随机推荐
- DIV+CSS+JS实现图片<ul><li></li></ul>无缝滚动代码
			
(含上下左右滚动代码) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...
 - requirejs——define——普通模块
			
一.普通模块可能包含的内容: 一个模块对应着一个js文件,由于模块可能包含着以下三种内容:模块名.依赖模块.返回给其他模块使用的参数:因此js文件根据包含内容的不同而写法不同. 一.传统的js脚本文件 ...
 - 虚拟机之 LAMP
			
LAMP 就是Linux apache mysql php 一.下载: 安装下载工具 yum install wget -y mysql:5.5.47 wget http://mirrors.sohu ...
 - PHP自动加载配置ArrayAccess类
			
ArrayAccess是PHP的类,可以把对象当成数组来使用访问. Config.php 配置类 <?php namespace IMooc; class Config implements ...
 - while 和do while循环的区别
			
int a; scanf_s("%d",&a); while(a>0) { //do something; } while循环先要判断条件是否成立,如果不成立,那么就 ...
 - 多个if和一个ifelse的区别
			
一个程序的要求如下,输入一个学生的数学成绩,如果大于等于60,那么就输出good,如果小于60那么输出not good int a scanf_s("%d",&a) if( ...
 - 数字图像处理实验(11):PROJECT 05-02,Noise Reduction Using a Median Filter                                                                                                         标签:               图像处理MATLAB                                            2017-05-26 23:
			
实验要求: Objective: To understand the non-linearity of median filtering and its noise suppressing abili ...
 - Django--admin后台
			
需求 通过后台和models操作数据库表 实现 1.后台中看到数据库中的表 app01/admin.py 1 2 from app01 import models admin.site.regist ...
 - Browsersync 简介 and 使用
			
简介 省时的浏览器同步测试工具,Browsersync能让浏览器实时.快速响应您的文件更改(html.js.css.sass.less等)并自动刷新页面. 曾经我们每改一次的代码,都需要手动去刷新一次 ...
 - JPA注解解析
			
最近学习hibernate注解形式配置POJO类,将注解的解析记下来,以备以后使用. 例1. @Entity@Table(name="user") public class Fl ...