网络流

21 篇文章 0 订阅
3 篇文章 0 订阅

Dinic:

var	t,cap,next:array[0..400] of longint;
	headlist:array[0..200] of longint;
	vis:array[0..200] of boolean;
	cur,d:array[0..200] of longint;
	queue:array[0..200] of longint;
	ans,front,finish,num,i,j,k,m,n,x,y,z:longint;
procedure init;
begin
	readln(m,n);
	for i:=1 to n do
		headlist[i]:=-1;
	num:=-1;
	for i:=1 to m do
	begin
		readln(x,y,z);
		inc(num);
		next[num]:=headlist[x];
		headlist[x]:=num;
		t[num]:=y;
		cap[num]:=z;
		inc(num);
		next[num]:=headlist[y];
		headlist[y]:=num;
		t[num]:=x;
		cap[num]:=0;
	end;
end;
function BFS:boolean;
var i:longint;
begin
	fillchar(vis,sizeof(vis),false);
	queue[1]:=1; front:=1; finish:=1;
	vis[1]:=true;
	while (finish>=front) do
	begin
		i:=headlist[queue[front]];
		while i<>-1 do
		begin
			if (not(vis[t[i]]) and (cap[i]>0)) then begin
						vis[t[i]]:=true;
						d[t[i]]:=d[queue[front]]+1;
						inc(finish);
						queue[finish]:=t[i];
													end;
			i:=next[i];
		end;
		inc(front);
	end;
	exit(vis[n]);
end;
function min(a,b:longint):longint;
begin
	if a<b then exit(a);
	exit(b);
end;
function DFS(now,a:longint):longint;
var flow,f,i:longint;
begin
	if (now=n) or (a=0) then exit(a);
	flow:=0;
	i:=cur[now];
	while i<>-1 do
	begin
		if d[now]+1=d[t[i]] then begin
			f:=dfs(t[i],min(a,cap[i]));
			if f>0 then begin
							cap[i]:=cap[i]-f;
							cap[i xor 1]:=cap[i xor 1]+f;
							flow:=flow+f;
							a:=a-f;
							if (a=0) then break;
						end;
								end;
		i:=next[i];
		cur[now]:=i;
	end;
	if flow=0 then d[now]:=-1;
	exit(flow);
end;
procedure Dinic;
begin
	ans:=0;
	while BFS do
	begin
		for i:=1 to n do
			cur[i]:=headlist[i];
		ans:=ans+dfs(1,maxlongint);
	end;
end;
begin
	init;
	Dinic;
	writeln(ans);
end.
ISAP:

const shuru='NetFlow.in';
	  shuchu='NetFlow.out';
type pppp=record
		from,too,sap:longint;
		  end;
var	edge:array[0..401] of pppp;
	g:Array[0..200,0..200] of longint;
	queue,sum,p,cur,d:array[0..201] of longint;
	vis:array[0..201] of boolean;
	ans,front,finish,now,x,y,z,num,i,j,k,n,m:longint;
	ok:boolean;
procedure init;
begin
	readln(m,n);
	num:=-1;
	for i:=1 to m do
	begin
		readln(X,y,z);
		inc(g[x,0]);
		inc(num);
		g[x,g[x,0]]:=num;
		edge[num].from:=x;
		edge[num].too:=y;
		edge[num].sap:=z;
		inc(g[y,0]);
		inc(num);
		g[y,g[y,0]]:=num;
		edge[num].from:=y;
		edge[num].too:=x;
		edge[num].sap:=0;
	end;
end;
procedure BFS;
var i:longint;
	step:pppp;
begin
	fillchar(vis,sizeof(vis),false);
	queue[1]:=n; front:=1; finish:=1; vis[n]:=true;
	while finish>=front do
	begin
		for i:=1 to g[queue[front],0] do
		begin
			step:=edge[g[queue[front],i]];
			if not(vis[step.too]) then begin
					inc(Finish);
					queue[finish]:=step.too;
					vis[step.too]:=true;
					d[step.too]:=d[step.from]+1;
														end;
		end;
		inc(front);
	end;
end;
function min(a,b:longint):longint;
begin
	if a<b then exit(A);
	exit(b);
end;
function Augment:longint;
var x,a:longint;
begin
	x:=n; a:=maxlongint;
	while x<>1 do
	begin
		a:=min(a,edge[p[x]].sap);
		x:=edge[p[x]].from;
	end;
	x:=n;
	while x<>1 do
	begin
		edge[p[x]].sap:=edge[p[x]].sap-a;
		edge[p[x] xor 1].sap:=edge[p[x] xor 1].sap+a;
		x:=edge[p[x]].from;
	end;
	exit(A);
end;
procedure ISAP;
begin
	BFS;
	for i:=1 to n do inc(sum[d[i]]);
	x:=1; ans:=0;
	for i:=1 to n do cur[i]:=1;
	while (d[1]<n) do
	begin
		if (x=n) then begin
						ans:=ans+Augment;
						x:=1;
					  end;
		OK:=false;
		for i:=cur[x] to g[x,0] do
			if (d[x]=d[edge[g[x,i]].too]+1) and (edge[g[x,i]].sap>0) then begin
						ok:=true;
						p[edge[g[x,i]].too]:=g[x,i];
						cur[x]:=i;
						x:=edge[g[x,i]].too;
						break;
																		  end;
		if not(ok) then begin
				m:=n-1;
				for i:=1 to g[x,0] do
					if edge[g[x,i]].sap>0 then m:=min(m,d[edge[g[x,i]].too]);
				dec(sum[d[x]]);
				if sum[d[x]]=0 then break;
				d[x]:=m+1;
				inc(sum[d[x]]);
				cur[x]:=1;
				if (x<>1) then x:=edge[p[x]].from;
						end;
	end;
end;
begin
	init;
	ISAP;
	writeln(Ans);
end.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值