什么时候ZJ省选再现一次这么良心的题吧...

题意:

  在一个染色的格子画分割线,使其不想连,求最少的线段

SOL:

  裸裸的最小割.题目要求两种颜色不想连,我们把他分到两个集合,也就是把所有相连的边切断-----这不就是最小割嘛. 把其中一个颜色与源相连,另一个颜色与汇相连,容量为正无穷,然后中间相连的容量均为1,然后跑下dinic即可.

Code:

  

/*==========================================================================
# Last modified: 2016-03-11 18:09
# Filename: 1412.cpp
# Description:
==========================================================================*/
#define me AcrossTheSky
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 10005
#define maxm 100000
#define pi 3.1415926535898
#define _e 2.718281828459
#define inf 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num) {
bool start=false,neg=false;
char c;
num=0;
while((c=getchar())!=EOF) {
if(c=='-') start=neg=true;
else if(c>='0' && c<='9') {
start=true;
num=num*10+c-'0';
} else if(start) break;
}
if(neg) num=-num;
}
/*==================split line==================*/
using namespace std;
int first[maxn],d[maxn],cur[maxn];
bool vis[maxn];
int cnt=1,n,m;
int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0},mp[105][105];
struct data{int to,next,v;}e[500001];
int T,S;
void ins(int u,int v,int w)
{e[++cnt].to=v;e[cnt].next=first[u];e[cnt].v=w;first[u]=cnt;}
void insert(int u,int v,int w)
{ins(u,v,w);ins(v,u,0);} int bfs(){
queue<int> q;
for(int i=S;i<=T;i++) vis[i]=false;
q.push(0); d[0]=0; vis[0]=true;
while (!q.empty()){
int now=q.front(); q.pop();
for (int i=first[now];i;i=e[i].next)
if (!vis[e[i].to] && e[i].v){
d[e[i].to]=d[now]+1;
vis[e[i].to]=true;
q.push(e[i].to);
}
}
return vis[T];
}
int dfs(int now,int a){
if (now==T || !a) return a;
int f,flow=0;
for (int & i=cur[now];i;i=e[i].next)
if (d[now]+1==d[e[i].to] && (f=dfs(e[i].to,min(a,e[i].v)))>0){
flow+=f; a-=f; e[i].v-=f; e[i^1].v+=f;
if (!a) break;
}
return flow; }
int dinic(){
int ans=0;
while(bfs()){
FORP(i,0,T) cur[i]=first[i];
ans+=dfs(0,inf);
}
return ans;
}
void init()
{
read(n); read(m);
T=n*m+1,S=0;
FORP(i,1,n)
FORP(j,1,m) read(mp[i][j]);
}
void build()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(mp[i][j]==1)insert(0,(i-1)*m+j,inf);
else if(mp[i][j]==2)insert((i-1)*m+j,T,inf);
for(int k=0;k<4;k++)
{
int nowx=i+xx[k],nowy=j+yy[k];
if(nowx<1||nowx>n||nowy<1||nowy>m||mp[i][j]==2)continue;
if(mp[i][j]!=1||mp[nowx][nowy]!=1)
insert((i-1)*m+j,(nowx-1)*m+nowy,1);
}
}
}
int main()
{
init();
build();
printf("%d",dinic());
return 0;
}

BZOJ 1412 & 最小割的更多相关文章

  1. bzoj 1412 最小割 网络流

    比较明显的最小割建模, 因为我们需要把狼和羊分开. 那么我们连接source和每个羊,流量为inf,代表这条边不能成为最小割中的点,同理连接每个狼和汇,流量为inf,正确性同上,那么对于每个相邻的羊和 ...

  2. BZOJ 1797 最小割

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1797 题意:给出一个有向图,每条边有流量,给出源点汇点s.t.对于每条边,询问:(1)是 ...

  3. BZOJ 2229 最小割

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2229 题意:给定一个带权无向图.若干询问,每个询问回答有多少点对(s,t)满足s和t的最 ...

  4. bzoj 1497 最小割模型

    我们可以对于消费和盈利的点建立二分图,开始答案为所有的盈利和, 那么源向消费的点连边,流量为消费值,盈利向汇连边,流量为盈利值 中间盈利对应的消费连边,流量为INF,那么我们求这张图的最小割,用 开始 ...

  5. bzoj 1934 最小割

    收获: 1.流量为0的边可以不加入. 2.最小割方案要与决策方案对应. #include <cstdio> #include <cmath> #include <cstr ...

  6. bzoj 3996 最小割

    公式推出来后想了半天没思路,居然A是01矩阵..... 如果一个问题是求最值,并那么尝试先将所有可能收益加起来,然后矛盾部分能否用最小割表达(本题有两个矛盾,第一个是选还是不选,第二个是i,j有一个不 ...

  7. bzoj 1934最小割

    比较显然的最小割的题,增加节点source,sink,对于所有选1的人我们可以(source,i,1),选0的人我们可以(i,sink,1),然后对于好朋友我们可以连接(i,j,1)(j,i,1),然 ...

  8. bzoj 1497 最小割

    思路:最小割好难想啊,根本想不到.. S -> 用户群 = c[ i ] 基站 -> T = p[ i ] 用户群 -> a[ i ] = inf 用户群 -> b[ i ] ...

  9. BZOJ 1797 最小割(最小割割边唯一性判定)

    问题一:是否存在一个最小代价路径切断方案,其中该道路被切断? 问题二:是否对任何一个最小代价路径切断方案,都有该道路被切断? 现在请你回答这两个问题. 最小割唯一性判定 jcvb: 在残余网络上跑ta ...

随机推荐

  1. 第一个JAVA创建

    1.file-new-java project  创建项目文件夹 2.在项目文件夹new-class 3.java对大小写比较敏感 输入代码 public class HELLOWORD { publ ...

  2. Mac使用入门

    mac常用快捷键 全屏/退出全屏 ctr+command+F 切换到桌面 fn+f11 输入法切换 ctr+空格 亮度 f1.f2 声音 f11.f12 复制.粘贴 command+c.command ...

  3. sql语句的join用法

    sql的join分为三种,内连接.外连接.交叉连接. 以下先建2张表,插入一些数据,后续理解起来更方便一些. create table emp(empno int, name char(20),dep ...

  4. Android 中this、getContext()、getApplicationContext()、getApplication()、getBaseContext() 之间的区别

    : 知之为知之,不知为不知是知也! 使用this, 说明当前类是context的子类,一般是activity application等; this:代表当前,在Activity当中就是代表当前的Act ...

  5. Platform SDK、Windows SDK简介

    Platform SDK及Windows SDK是由微软公司出品的一个软件开发包,向在微软的Windows操作系统和.NET框架上开发软件和网站的程序员提供头文件.库文件.示例代码.开发文档和开发工具 ...

  6. [javascript] 使用闭包编写模块

    这是一篇[javascript the good parts]的读书笔记. 我们知道可以利用javascript 的prototype 特性为原始类型编写拓展模块.利用如下方法: Object.pro ...

  7. "Project facet Java version 1.7 is not supported"的问题解决的办法

    问题描述 在eclipse中,从SVN中检出project代码,拖拽式部署到local server中的时候,报出以下错误: 问题分析 问题产生的原因是,SVN中的代码是采用java 1.7开发编译的 ...

  8. wpf window set window的owner

        [DllImport("user32.dll")]   public static extern IntPtr GetAncestor(IntPtr hWnd, int f ...

  9. 异步框架asyn4j的原理

    启动时调用init方法 public void init(){ if (!run){ run = true; //工作队列 workQueue = newPriorityBlockingQueue(m ...

  10. Uva 11059 Maximum Product

    注意long long  long long  longlong !!!!!!   还有 printf的时候 明明longlong型的答案 用了%d  WA了也看不出,这个细节要注意!!! #incl ...