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 上传者: 匿名 提交提交记录统计讨论测试数据 ...
随机推荐
- $http questions
//////1 $http.get("/api/task/165/").then( function(a){ console.log(a); }, function(b){ con ...
- Aptana Studio 3 如何汉化,实现简体中文版
这篇文章写了又一年多的时间了,哈哈,今天更新一次 此处修正的下面教程的[第五步]Babel Language Pack Update Site for Helioshttp://download.ec ...
- 简单的方法实现仿微信底部的Tab选项卡界面
在网上看了比较多的关于Tab的教程,发现都很杂乱.比较多的用法是用TitlePagerTabStrip和ViewPaper.不过TitlePagerTabStrip有个很大的缺陷,Tab里面的内容刚进 ...
- vue简单路由(二)
在实际项目中我们会碰到多层嵌套的组件组合而成,但是我们如何实现嵌套路由呢?因此我们需要在 VueRouter 的参数中使用 children 配置,这样就可以很好的实现路由嵌套. index.html ...
- idata的各个类型
idata是51系列单片机能识别的存储器类型之一,固定指前面0x00-0xff的256个字节的片内RAM,其中前128字节和data的128字节完全相同,只是因为访问的方式不同.idata是用类似C中 ...
- 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.4 绘制数据图
5.4 绘制数据图 参考视频: 5 - 4 - Plotting Data (10 min) 5.4.1 绘制曲线 1.画一个sin曲线 >> t = [:0.01:0.98]; > ...
- dyld_decache&MesaSQLite
[dyld_decache] Starting from iPhone OS 3.1, the individual libraries files supplied by the system ar ...
- Spring总结九:事务管理机制
何为事务 事务(Transaction),一般是指要做的或所做的事情.在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库操纵语言或编程语言(如SQL ...
- CentOS7虚拟机安装Linux教程及安装后所遇到的问题
1.VMware Workstation15下载. 官方链接:http://download3.vmware.com/software/wkst/file/VMware-workstation-ful ...
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...