POJ2455 Secret Milking Machine ——二分答案+网络流

本文介绍了一种使用二分答案和网络流技术解决特定路径问题的方法,详细解释了算法实现过程和核心步骤,包括构建权比二分答案小的边的网络、网络流判定以及优化算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  思路:二分答案,用权值比二分出来的答案小的边来建图。网络流判定的是是否能够满足找到t条路径。

CODE

Program Secret;//By_Poetshy
Const
	maxn=40000;
Var
	i,j,k,m,n,p,t,d			:Longint;
	pre,other,last,data		:Array[1..maxn*2]of Longint;
	a,b						:Array[1..200]of Longint;
	v						:Array[1..200]of Boolean;
	f,c						:Array[1..200,1..200]of Longint;
	l,r,mid,ans,max			:Longint;

Function Min(i,j:Longint):Longint;
begin
	if i<j then exit(i);exit(j);
end;

Function Check(i:Longint):Boolean;
var j,k,p,q,sum,head,tail	:Longint;
	seq						:Array[1..maxn]of Longint;
begin
	fillchar(f,sizeof(f),0);
	fillchar(c,sizeof(c),0);
	for p:=1 to n do
		begin
			j:=last[p];
			while j<>0 do
				begin
					k:=other[j];
					if data[j]<=i then inc(c[p,other[j]]);
					j:=pre[j];
				end;
		end;
	repeat
		fillchar(v,sizeof(v),0);
		head:=0;tail:=1;seq[1]:=1;
		a[1]:=0;b[1]:=maxlongint;
		v[1]:=true;
		while head<tail do
			begin
				inc(head);p:=seq[head];
				for q:=1 to n do
					if (c[p,q]<>0)and(f[p,q]<c[p,q])and(not v[q])then
						begin
							v[q]:=true;
							a[q]:=p;b[q]:=Min(c[p,q]-f[p,q],b[p]);
							inc(tail);seq[tail]:=q;
						end;
				if seq[tail]=n then break;
			end;
		if v[n] then
			begin
				p:=n;
				while p<>0 do
					begin
						if a[p]<>0 then inc(f[a[p],p],b[n]);
						p:=a[p];
					end;
			end;
	until not v[n];
	sum:=0;
	for p:=1 to n-1 do inc(sum,f[p,n]);
	if sum>=t then exit(true);
	exit(false);
end;

BEGIN
	readln(n,p,t);
	for i:=1 to p do
		begin
			readln(m,j,d);
			inc(k);pre[k]:=last[m];last[m]:=k;other[k]:=j;data[k]:=d;
			inc(k);pre[k]:=last[j];last[j]:=k;other[k]:=m;data[k]:=d;
			if d>max then max:=d;
		end;
	l:=0;r:=max;
	while l<=r do
		begin
			mid:=(l+r)>>1;
			if check(mid) then
				begin
					ans:=mid;
					r:=mid-1;
				end else l:=mid+1;
		end;
	writeln(ans);
END.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值