1412: [ZJOI2009]狼和羊的故事
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 4017 Solved: 2037
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
2 2
1 1
Sample Output
数据范围
10%的数据 n,m≤3
30%的数据 n,m≤20
100%的数据 n,m≤100
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; #define get(i,j) ((i-1)*m+j)
const int INF=0x7f7f7f7f;
const int MAXN=;
const int xx[]={-,,,},yy[]={,,-,}; struct Edge
{
int to,w,next;
}E[MAXN];
int node=,head[MAXN],dis[MAXN],mp[][];
int s=,t=;
int n,m,ans; void insert(int u,int v,int w)
{
E[++node]=(Edge){v,w,head[u]};
head[u]=node;
E[++node]=(Edge){u,,head[v]};
head[v]=node;
} bool bfs()
{
memset(dis,-,sizeof(dis));
queue<int> Q;
Q.push(s);
dis[s]=;
while(!Q.empty())
{
int q=Q.front();Q.pop();
for(int i=head[q];i;i=E[i].next)
if(E[i].w&&dis[E[i].to]==-)
{
Q.push(E[i].to);
dis[E[i].to]=dis[q]+;
}
}
return dis[t]!=-;
} int dfs(int x,int flow)
{
if(x==t) return flow;
int w,used=;
for(int i=head[x];i;i=E[i].next)
if(E[i].w&&dis[E[i].to]==dis[x]+)
{
w=flow-used;
w=dfs(E[i].to,min(w,E[i].w));
E[i].w-=w;
E[i^].w+=w;
used+=w;
if(used==flow)return flow;
}
if(!used) dis[x]=-;
return used;
} void dinic()
{
while(bfs()) ans+=dfs(s,INF);
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==) insert(s,get(i,j),INF);
else if(mp[i][j]==) insert(get(i,j),t,INF);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
for(int k=;k<;k++)
{
int x=i+xx[k],y=j+yy[k];
if(x<||x>n||y<||y>m) continue;
if(mp[i][j]!=||mp[x][y]!=)
insert(get(i,j),get(x,y),);
}
dinic();
printf("%d",ans);
return ;
}
1412: [ZJOI2009]狼和羊的故事的更多相关文章
- BZOJ 1412: [ZJOI2009]狼和羊的故事( 最小割 )
显然是最小割...把狼的领地连S, 羊的领地连T, 然后中间再连边, 跑最大流就OK了 -------------------------------------------------------- ...
- 1412. [ZJOI2009]狼和羊的故事【最小割】
Description “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! O ...
- BZOJ 1412: [ZJOI2009]狼和羊的故事【网络流】
Description “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! O ...
- bzoj 1412 [ZJOI2009]狼和羊的故事(最小割)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1412 [题意] 在一个n*m的格子中,将羊和狼隔开的最小代价. [思路] 最小割. 由 ...
- bzoj 1412: [ZJOI2009]狼和羊的故事
http://www.lydsy.com/JudgeOnline/problem.php?id=1412 超级源点连向所有的狼,超级汇点连向所有羊,流量为INF 相邻连边流量为1,最小割 #inclu ...
- BZOJ 1412 [ZJOI2009]狼和羊的故事 | 网络流
显然是个最小割嘛! 一开始我是这么建图的: 源点向狼连INF 羊向汇点连INF 每两个相邻格子间连双向边,边权为1 然后T成狗 后来我是这么建图的: 源点向狼连INF 羊向汇点连INF 狼和空地向相邻 ...
- bzoj1412: [ZJOI2009]狼和羊的故事
空地之间开始没有连然后一直WA...题意混乱...尴尬. #include<cstdio> #include<cstring> #include<iostream> ...
- BZOJ1412 [ZJOI2009]狼和羊的故事 【最小割】
1412: [ZJOI2009]狼和羊的故事 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3454 Solved: 1733 [Submit][ ...
- P2598 [ZJOI2009]狼和羊的故事(网络流)
P2598 [ZJOI2009]狼和羊的故事 源点和所有狼连 $inf$ 的边 所有羊和汇点连 $inf$ 的边 所有点向四周连 $1$ 的边 这样所有狼和羊之间的边都被割掉了 统计最小割就好辣 #i ...
随机推荐
- Sublime Text 3 多行游标
选中要修改的地方ctrl+D ,要跳过不需要修改的选中的就用ctrl+k+d 选中要修改的地方ctrl+D,选中所有要修改的 alt+f3 ctrl+A ,然后ctrl+shift+L 按住shif ...
- POJ1040 Transportation
题目来源:http://poj.org/problem?id=1040 题目大意: 某运输公司要做一个测试.从A城市到B城市的一条运输线路中有若干个站,将所有站包括A和B在内按顺序编号为0到m.该路线 ...
- Subversion Server Edge用户权限设置简介
Subversion Server Edge用户权限可分为两种,一种为按用户权限,一种为按组权限设置 1.按用户设置权限 [codeLibrary:/] //对整个代码库 *=r //所有用户 ...
- 汇编语言版本的HelloWorld
平台 macOS 工具 nasm clang 文件 main.asm extern _printf ; 这里调用系统的一个系统调用函数, _printf, 使用extern告诉链接器该label在其他 ...
- ACdream 1427—— Nice Sequence——————【线段树单点更新,区间查询】
Nice Sequence Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Su ...
- hdu 5489——Removed Interval——————【删除一段区间后的LIS】
Removed Interval Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- dell电脑 win8换win7重启报错及解决方案
Win8换win7 bios 识别不到usb选项 按以下操作即可: 把Secure Boot control 改为Disabled 的,F10保存重启,F12进入bios选择usb启动即可: 安装完系 ...
- liunx下文件授权可执行权限chmod
Cannot find ./catalina.sh The file is absent or does not have execute permission This file is needed ...
- 使用compiz出现奔溃的一些应急办法
Linux Mint 17.1 CompizConfig is also installed by default so you can configure every aspect of Compi ...
- Counting blessings can actually increase happiness and health by reminding us of the good things in life.
Counting blessings can actually increase happiness and health by reminding us of the good things in ...