BZOJ1412[ZJOI2009]狼和羊的故事——最小割
题目描述
输入
输出
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
int next[1000001];
int to[1000001];
int val[1000001];
int head[1000001];
int tot=1;
int q[1000001];
int s[120][120];
int n,m;
int S,T;
int ans;
int d[1000001];
const int INF=0x3f3f3f3f;
void add(int x,int y,int v)
{
tot++;
next[tot]=head[x];
head[x]=tot;
to[tot]=y;
val[tot]=v;
tot++;
next[tot]=head[y];
head[y]=tot;
to[tot]=x;
val[tot]=0;
}
bool bfs(int S,int T)
{
int r=0;
int l=0;
memset(d,-1,sizeof(d));
q[r++]=S;
d[S]=0;
while(l<r)
{
int now=q[l];
for(int i=head[now];i;i=next[i])
{
if(d[to[i]]==-1&&val[i]!=0)
{
d[to[i]]=d[now]+1;
q[r++]=to[i];
}
}
l++;
}
if(d[T]==-1)
{
return false;
}
else
{
return true;
}
}
int dfs(int x,int flow)
{
if(x==T)
{
return flow;
}
int now_flow;
int used=0;
for(int i=head[x];i;i=next[i])
{
if(d[to[i]]==d[x]+1&&val[i]!=0)
{
now_flow=dfs(to[i],min(flow-used,val[i]));
val[i]-=now_flow;
val[i^1]+=now_flow;
used+=now_flow;
if(now_flow==flow)
{
return flow;
}
}
}
if(used==0)
{
d[x]=-1;
}
return used;
}
void dinic()
{
while(bfs(S,T)==true)
{
ans+=dfs(S,INF);
}
}
int main()
{
scanf("%d%d",&n,&m);
S=n*m+1;
T=n*m+2;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&s[i][j]);
if(s[i][j]==2)
{
add(m*(i-1)+j,T,INF);
}
else if(s[i][j]==1)
{
add(S,m*(i-1)+j,INF);
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int x=(i-1)*m+j;
if(s[i][j]==1||s[i][j]==0)
{
if(i-1>=1&&(s[i-1][j]==2||s[i-1][j]==0))
{
add(x,m*(i-2)+j,1);
}
if(j-1>=1&&(s[i][j-1]==2||s[i][j-1]==0))
{
add(x,m*(i-1)+j-1,1);
}
if(i+1<=n&&(s[i+1][j]==2||s[i+1][j]==0))
{
add(x,m*i+j,1);
}
if(j+1<=m&&(s[i][j+1]==2||s[i][j+1]==0))
{
add(x,m*(i-1)+j+1,1);
} }
}
}
dinic();
printf("%d",ans);
return 0;
}
BZOJ1412[ZJOI2009]狼和羊的故事——最小割的更多相关文章
- 【BZOJ1412】[ZJOI2009]狼和羊的故事 最小割
[BZOJ1412][ZJOI2009]狼和羊的故事 Description “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想: ...
- BZOJ 1412: [ZJOI2009]狼和羊的故事( 最小割 )
显然是最小割...把狼的领地连S, 羊的领地连T, 然后中间再连边, 跑最大流就OK了 -------------------------------------------------------- ...
- P2598 [ZJOI2009]狼和羊的故事(最小割)
P2598 [ZJOI2009]狼和羊的故事 说真的,要多练练网络流的题了,这么简单的网络流就看不出来... 题目要求我们要求将狼和羊分开,也就是最小割,(等等什么逻辑...头大....) 我们这样想 ...
- [ZJOI2009] 狼与羊的故事 - 最小割
给定一个\(N \times M\)方格矩阵,每个格子可在\(0,1,2\)中取值.要求在方格的边上进行划分,使得任意联通块内不同时包含\(1\)和\(2\)的格子. ________________ ...
- BZOJ1412 ZJOI2009 狼和羊的故事 【网络流-最小割】
BZOJ1412 ZJOI2009 狼和羊的故事 Description “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和 ...
- BZOJ1412 [ZJOI2009]狼和羊的故事 【最小割】
1412: [ZJOI2009]狼和羊的故事 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3454 Solved: 1733 [Submit][ ...
- bzoj1412: [ZJOI2009]狼和羊的故事
空地之间开始没有连然后一直WA...题意混乱...尴尬. #include<cstdio> #include<cstring> #include<iostream> ...
- LG2598/BZOJ1412 「ZJOI2009」狼和羊的故事 最小割
问题描述 LG2598 BZOJ1412 题解 看到要把狼和羊两个物种分开 自然想到最小割. 发现\((x,y)\)可以向上下左右走以获得贡献,所以建边:\((x,y),(x-1,y)\),\((x, ...
- bzoj1412: [ZJOI2009]狼和羊的故事(最小割)
传送门 首先,考虑只有狼和羊怎么办.我们把源点向所有羊连边,容$inf$,所有狼向汇点连边,容$inf$,然后羊向周围所有的狼连边,容$1$.那么,只要求一个割就能把狼和羊给分开,求一个最小割就是答案 ...
随机推荐
- ML.NET 示例:回归之价格预测
写在前面 准备近期将微软的machinelearning-samples翻译成中文,水平有限,如有错漏,请大家多多指正. 如果有朋友对此感兴趣,可以加入我:https://github.com/fei ...
- 深入理解Redis复制
复制 A few things to understand ASAP about Redis replication. 1) Redis replication is asynchronous, bu ...
- go项目
1.循环使用缓存 每条日志需要开辟缓存块来存储内容,以减少频繁的内存分配与回收.日志结构体定义如下: type MLogger struct { // freeList is a list of by ...
- Arduino Core For ESP8266
如果选择纯C作为ESP8266的开发,有两个途径: 使用乐鑫官方原生的 RTOS-SDK或者NONOS-SDK 使用Arduino IDE 使用PlatformIO 作为一个"Arduino ...
- LeetCode 832. Flipping an Image
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...
- 使用 Markdown编辑
作用: 学习笔记,整理日志, 发布日记,杂文,所见所想 撰写发布技术文稿(代码支持) 撰写发布学术论文(LaTeX 公式支持) sublime text3插件 输入 Shift + Ctrl + P, ...
- c++入门之初话结构体
结构体是一种具有一定数据结构思想的数据类型,我们在对待结构体的时候,用该从数据结构的思想去审视结构体.下面给出结构体的定义 struct mystruct {]; int score; double ...
- Comet OJ 热身赛(E题)(处理+最短路算法)
dijkstra 已经提交 已经通过 42.86% Total Submission:189 Total Accepted:81 题目描述 Eagle Jump公司正在开发一款新的游戏.泷本一二三作为 ...
- R语言绘制直方图,
直方图: 核密度函数: 练习题目1: 绘制出15位同学体重的直方图和核密度估计图,并与正态分布的概率密度函数作对比 代码如下: > w <- c(75.0, 64.0, 47.4, 66. ...
- servlet程序HTTP Status 500 - Error instantiating servlet class 解决
在eclipase 中编译 servlet 但是一致报 HTTP Status 500 - Error instantiating servlet class XXX类 的问题 , 解决方法 1. ...