算法】二分图匹配(最大流)

【题解】对i+j进行奇偶染色,就可以保证相邻两格异色。

然后就是二分图了,对相邻格子连边跑最大流即可。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=,maxN=,inf=0x3f3f3f3f;
struct edge{int from,v,flow;}e[];
int n,m,k,tot=,first[maxN],p[maxn][maxn],cnt,d[maxN],q[],S,T,cur[maxN];
long long ans;
void insert(int u,int v,int flow)
{
tot++;e[tot].v=v;e[tot].flow=flow;e[tot].from=first[u];first[u]=tot;
tot++;e[tot].v=u;e[tot].flow=;e[tot].from=first[v];first[v]=tot;
}
bool bfs()
{
memset(d,-,sizeof(d));
int head=,tail=;q[]=S;d[]=;
while(head!=tail)
{
int x=q[head++];if(head>=)head=;
for(int i=first[x];i;i=e[i].from)
if(e[i].flow&&d[e[i].v]==-)
{
d[e[i].v]=d[x]+;
q[tail++]=e[i].v;if(tail>=)tail=;
}
}
return d[T]!=-;
}
int dfs(int x,int a)
{
if(x==T||a==)return a;
int flow=,f;
for(int i=first[x];i;i=e[i].from)
if(d[e[i].v]==d[x]+&&(f=dfs(e[i].v,min(a,e[i].flow)))>)
{
e[i].flow-=f;
e[i^].flow+=f;
a-=f;
flow+=f;
if(a==)break;
}
return flow;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
S=;
for(int i=;i<=k;i++)
{
int u,v;
scanf("%d%d",&u,&v);
p[u][v]=-;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(p[i][j]!=-)
{
p[i][j]=++cnt;
}
}
T=++cnt;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(p[i][j]!=-)
{
if((i+j)&)
{
if(i>&&p[i-][j]>)insert(p[i][j],p[i-][j],);
if(i<n&&p[i+][j]>)insert(p[i][j],p[i+][j],);
if(j>&&p[i][j-]>)insert(p[i][j],p[i][j-],);
if(j<n&&p[i][j+]>)insert(p[i][j],p[i][j+],);
insert(S,p[i][j],);
}
else insert(p[i][j],T,);
}
}
ans=;
while(bfs())
{
for(int i=;i<=tot;i++)cur[i]=first[i];
ans+=dfs(S,inf);
}
printf("%lld",ans);
return ;
}

【CODEVS】1022 覆盖的更多相关文章

  1. Codevs 1022 覆盖

    时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 有一个N×M的单位方格中,其中有些方格是水塘,其他方格是陆地.如果要用1×2的矩 ...

  2. Code[VS] 1022 覆盖 题解

    Code[VS] 1022 覆盖 题解  Hungary Algorithm 题目传送门:Code[VS] 1022 题目描述 Description 有一个N×M的单位方格中,其中有些方格是水塘,其 ...

  3. 【wikioi】1022 覆盖(匈牙利)

    http://www.wikioi.com/problem/1022/ 好不容易来一次1A,,水题啊.. 染色后裸匈牙利orz #include <cstdio> #include < ...

  4. CodeVS 线段覆盖1~5

    #include <bits/stdc++.h> using namespace std; ; struct Info{int l,r;}P[Maxn]; int n,Cnt,F[Maxn ...

  5. codevs 版刷计划(1000-1099)

    Diamond咋都是模板题... 开个坑刷codevs的Master题.巩固一下姿势. 目前AC的题目:1001,1021,1022, 1001.舒适的路线(并查集) 求出无向图s到t路径上的min( ...

  6. 【BZOJ-3589】动态树 树链剖分 + 线段树 + 线段覆盖(特殊的技巧)

    3589: 动态树 Time Limit: 30 Sec  Memory Limit: 1024 MBSubmit: 405  Solved: 137[Submit][Status][Discuss] ...

  7. 匈牙利算法实战codevs1022覆盖

    1022 覆盖    时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 有一个N×M的单位方格中 ...

  8. 【经典】Noip贪心

    一.经典例题 例一:排队接水 题意:n个人到r个水龙头接水,装满水桶的时间分别是t1,t2,t3.... 接水时间是整数且互不相等,怎样安排顺序使每个人等待的 时间和最小. 题解:排队越靠前计算次数越 ...

  9. codevs——T1214 线段覆盖

    http://codevs.cn/problem/1214/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

随机推荐

  1. 【转】Base64算法详解

    原文链接:https://blog.csdn.net/robertcpp/article/details/51628647 完整的BASE64定义可见RFC 1421和RFC 2045.编码后的数据比 ...

  2. 第100天:CSS3中animation动画详解

    CSS3属性中有关于制作动画的三个属性:Transform,Transition,Animation: 一.Animation定义动画 CSS3的Animation是由“keyframes”这个属性来 ...

  3. DIH增量、定时导入并检索数据--转载

    原文地址:http://www.ifunit.com/984/solr%E5%AD%A6%E4%B9%A0%EF%BC%88%E4%BA%94%EF%BC%89dih%E5%A2%9E%E9%87%8 ...

  4. 【Java】使用CSVUtils生成文件并供下载

    package com.msk.ds.logic; import java.io.*; import java.util.List; /** * Created by Administrator on ...

  5. 关于setInterval()定时

    最近项目中,遇到个需求就是获取停车场剩余车位数量,想是通过ajax定时抓取接口数据来实现(本想通过SignalR),但是项目本身直供少数人使用,感觉定时ajax可以满足 下面上代码 var handl ...

  6. poll() 与 select()比较

    比较poll() 与select() 尽管poll()和select()所做的是相同的工作,不过poll()优于select(),原因:    1.poll()不需要用户计算并传递作为参数的最高编号的 ...

  7. [洛谷P4503][CTSC2014]企鹅QQ

    题目大意:给你$n(n\leqslant3\times10^4)$个长度为$l(l\leqslant200)$的字符串,要你求出有多少对字符串是相似的,相似的定义是两个字符串只在一位上不同. 题解:可 ...

  8. Toast与Snackbar的那点事

    背景 Toast是Android平台上的常用技术.从用户角度来看,Toast是用户与App交互最基本的提示控件:从开发者角度来看,Toast是开发过程中常用的调试手段之一.此外,Toast语法也非常简 ...

  9. sql语句查询各门课程平均分的最大值

    解法一: select courseno,stuno,avg(score) '平均分最高值'--这里是求平均,后面的条件是过滤最大值的 from tablename group by courseno ...

  10. WPA-PSK无线网络破解原理及过程

    原文链接地址:http://www.freebuf.com/articles/wireless/58342.html 本文将主要讲讲WPA-PSK类型的无线网络安全问题,首先我们看下802.11协议相 ...