P4304 [TJOI2013]攻击装置(最大独立集)
发布日期:2021-06-30 10:31:48
浏览次数:2
分类:技术文章
本文共 1732 字,大约阅读时间需要 5 分钟。
问题可以抽象为图上选择最多的点,使得任意两个点间没有边
很明显的最大独立集
最大独立集=点数-最大匹配
所以这里需要抽象为二分图求最大匹配,发现按照马走日的走法,知道的点一定是性质不同的点
这个性质不同就是横坐标+纵坐标的奇偶性质
由奇数点连向偶数点构造二分图即可
#includeusing namespace std;const int maxn=5e6+10;const int inf=1e9;struct edge{ int to,nxt,flow;}d[maxn]; int head[maxn],cnt=1;void add(int u,int v,int flow){ d[++cnt]=(edge){ v,head[u],flow},head[u]=cnt; d[++cnt]=(edge){ u,head[v],0},head[v]=cnt;}int dis[maxn],n,s,t,ans;char a[1009][1009];bool bfs(int s,int t){ queue q; for(int i=0;i<=t;i++) dis[i]=0; dis[s]=1; q.push( s ); while( !q.empty() ) { int u=q.front(); q.pop(); for(int i=head[u];i;i=d[i].nxt ) { int v=d[i].to; if( dis[v]==0&&d[i].flow ) { dis[v]=dis[u]+1; q.push(v); if( v==t ) return true; } } } return false;}int dinic(int u,int flow){ if( u==t ) return flow; int res=flow; for(int i=head[u]; i&&res ;i=d[i].nxt ) { int v=d[i].to; if( d[i].flow&&dis[v]==dis[u]+1 ) { int temp=dinic(v,min(res,d[i].flow) ); if( temp==0 ) dis[v]=0; d[i].flow-=temp; d[i^1].flow+=temp; res-=temp; } } return flow-res;}int fx[10] = { 1,1,2,2,-1,-1,-2,-2};int fy[10] = { -2,2,-1,1,-2,2,-1,1};int id(int x,int y){ return (x-1)*n+y;}int main(){ cin >> n; for(int i=1;i<=n;i++) cin >> ( a[i]+1 ); s = 0, t = n*n*2+1; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if( a[i][j]=='0' ) ans++; if( a[i][j]=='1' ) continue; if( (i+j)&1 ) add( id(i,j)+n*n,t,1 ); else { add( s,id(i,j),1 ); for(int w=0;w<=7;w++) { int nx = i+fx[w], ny = j+fy[w]; if( nx<1 || nx> n || ny < 1 || ny > n ) continue; if( a[nx][ny] == '1' ) continue; add( id(i,j),id(nx,ny)+n*n,1 ); } } } while( bfs(s,t) ) ans -= dinic(s,inf); cout << ans;}
转载地址:https://issue-is-vegetable.blog.csdn.net/article/details/115370595 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月30日 06时05分46秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
summernote禁止图片视频上传
2019-04-30
1.Ribbon注册服务内部调用
2019-04-30
2.Openfeign注册服务内部调用
2019-04-30
1.gateway搭建
2019-04-30
1.Idea或eclipse 运行项目端口被占用处理
2019-04-30
1.JDK1.8新特性值Optional
2019-04-30
1.阿里云上安装jdk8
2019-04-30
2.阿里云上安裝redis
2019-04-30
3.阿里云上安装mysql
2019-04-30
4.阿里云上安装nacos
2019-04-30
6.阿里云上安装kafka
2019-04-30
5.阿里云上安装zookeeper
2019-04-30
7.阿里云上安装RabbitMQ
2019-04-30
JDK8 管道 Stream 详细使用介绍
2019-04-30
2.启停项目脚本
2019-04-30
配置了阿里云安全组端口,浏览器还是不能访问的问题
2019-04-30
mysql出表锁表MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
2019-04-30
cron表达式基础配置知识
2019-04-30
lombok基础使用
2019-04-30
jar启停服务
2019-04-30