bzoj 1412 最小割 网络流
比较明显的最小割建模, 因为我们需要把狼和羊分开。
那么我们连接source和每个羊,流量为inf,代表这条边不能成为最小割中的点,同理连接每个狼和汇,流量为inf,正确性同上,那么对于每个相邻的羊和狼,连接边,流量为1,代表隔开这两个点需要1的代价,对于每个空地和狼或者羊,连接边,流量为1,代表隔开这个两个点的代价为1,同时需要注意的是,对于空地之间的连边也应该是1,因为很有可能狼和羊通过空地相遇。这样做最大流就行了。
反思:手残将空地之间的连成inf了。。。
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ //By BLADEVIL
var
n, m :longint;
num, map :array[..,..] of longint;
pre, other, len :array[..] of longint;
last :array[..] of longint;
l :longint;
source, sink :longint;
go :array[..,..] of longint;
que, d :array[..] of longint;
ans :longint; procedure connect(x,y,z:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
len[l]:=z;
end; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; procedure init;
var
i, j, k :longint;
x :longint;
nx, ny :longint;
begin
go[,]:=-; go[,]:=; go[,]:=; go[,]:=-;
read(n,m);
l:=;
for i:= to n do
for j:= to m do num[i,j]:=(i-)*m+j; source:=n*m+; sink:=source+; for i:= to n do
for j:= to m do read(map[i,j]); for i:= to n do
for j:= to m do
begin
if map[i,j]= then
begin
connect(source,num[i,j],maxlongint);
connect(num[i,j],source,);
end else
if map[i,j]= then
begin
connect(num[i,j],sink,maxlongint);
connect(sink,num[i,j],);
end;
for k:= to do
begin
nx:=i+go[,k];
ny:=j+go[,k];
if (nx<) or (nx>n) or (ny<) or (ny>m) then continue;
if map[i,j]<>map[nx,ny] then
begin
connect(num[i,j],num[nx,ny],);
connect(num[nx,ny],num[i,j],);
end;
if (map[i,j]=map[nx,ny]) and (map[i,j]=) then
begin
connect(num[i,j],num[nx,ny],);
connect(num[nx,ny],num[i,j],);
end;
end;
end;
end; function bfs:boolean;
var
q, p :longint;
h, t, cur :longint;
begin
fillchar(d,sizeof(d),);
que[]:=source;
d[source]:=;
h:=; t:=;
while h<t do
begin
inc(h);
cur:=que[h];
q:=last[cur];
while q<> do
begin
p:=other[q];
if (len[q]>) and (d[p]=) then
begin
inc(t);
que[t]:=p;
d[p]:=d[cur]+;
if p=sink then exit(true);
end;
q:=pre[q];
end;
end;
exit(false);
end; function dinic(x,flow:longint):longint;
var
q, p :longint;
tmp, rest :longint;
begin
if x=sink then exit(flow);
rest:=flow;
q:=last[x];
while q<> do
begin
p:=other[q];
if (len[q]>) and (d[p]=d[x]+) and (rest>) then
begin
tmp:=dinic(p,min(rest,len[q]));
dec(rest,tmp);
dec(len[q],tmp);
inc(len[q xor ],tmp);
end;
q:=pre[q];
end;
exit(flow-rest);
end; procedure main;
begin
while bfs do
ans:=ans+dinic(source,maxlongint);
writeln(ans);
end; begin
init;
main;
end.
bzoj 1412 最小割 网络流的更多相关文章
- BZOJ 1412 & 最小割
什么时候ZJ省选再现一次这么良心的题吧... 题意: 在一个染色的格子画分割线,使其不想连,求最少的线段 SOL: 裸裸的最小割.题目要求两种颜色不想连,我们把他分到两个集合,也就是把所有相连的边切断 ...
- BZOJ 1797: [Ahoi2009]Mincut 最小割( 网络流 )
先跑网络流, 然后在残余网络tarjan缩点. 考虑一条边(u,v): 当且仅当scc[u] != scc[v], (u,v)可能出现在最小割中...然而我并不会证明 当且仅当scc[u] = scc ...
- 【bzoj1797】[Ahoi2009]Mincut 最小割 网络流最小割+Tarjan
题目描述 给定一张图,对于每一条边询问:(1)是否存在割断该边的s-t最小割 (2)是否所有s-t最小割都割断该边 输入 第一行有4个正整数,依次为N,M,s和t.第2行到第(M+1)行每行3个正 整 ...
- bzoj 2039 最小割模型
比较明显的网络流最小割模型,对于这种模型我们需要先求获利的和,然后减去代价即可. 我们对于第i个人来说, 如果选他,会耗费A[I]的代价,那么(source,i,a[i])代表选他之后的代价,如果不选 ...
- 最小割&网络流应用
重要链接 基础部分链接 : 二分图 & 网络流初步 zzz大佬博客链接 : 网络流学习笔记 重点内容:最小割二元关系新解(lyd's ppt) 题目:网络流相关题目 lyd神犇课件链接 : 网 ...
- BZOJ 1797 最小割
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1797 题意:给出一个有向图,每条边有流量,给出源点汇点s.t.对于每条边,询问:(1)是 ...
- BZOJ 2229 最小割
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2229 题意:给定一个带权无向图.若干询问,每个询问回答有多少点对(s,t)满足s和t的最 ...
- bzoj 1497 最小割模型
我们可以对于消费和盈利的点建立二分图,开始答案为所有的盈利和, 那么源向消费的点连边,流量为消费值,盈利向汇连边,流量为盈利值 中间盈利对应的消费连边,流量为INF,那么我们求这张图的最小割,用 开始 ...
- HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
随机推荐
- Python 3基础教程26-多行打印
本文来介绍多行打印.多行打印一般出现在欢迎界面,例如你玩过的游戏,第一个界面,很多文字显示. 我们随便打印几行,来模拟下这种多行打印情况. # 多行打印 print(''' 第一行内容 第二行内容 第 ...
- Sublime text3最全快捷键清单
[转]https://blog.csdn.net/mrchengzp/article/details/78508509,感谢作者的分享,收录方便查阅 Sublime Text 支持多种编程语言的语 ...
- flask - 1
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Worl ...
- HDU 4467 Graph(图论+暴力)(2012 Asia Chengdu Regional Contest)
Description P. T. Tigris is a student currently studying graph theory. One day, when he was studying ...
- POJ 1703 Find them, Catch them(并查集拓展)
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
- beta版本冲刺三
目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...
- Zebra - zebra command to get printer status
/// <summary> /// determine whether the network printer is in pause. /// </summary> /// ...
- oracle或mysql定时增量更新索引数据到Elasticsearch
利用kettle Spoon从oracle或mysql定时增量更新数据到Elasticsearch https://blog.csdn.net/jin110502116/article/details ...
- CSS兼容性总结
一.针对IE6的 !important 必须写在前面,例如: background:#9C6 !important;background:#999; 二.CSS HACK //IE6 专用 _heig ...
- ASP.NET页面之间传值QueryString(1)
QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法.但是对于传递数组或对象的话,就不能用这 ...