poj 2455 网络流最大值最小值

问题:点1 到点N之间  找出T条不重用trail 的path,并且求出这些path中,最长的trail长度的最小值.

思路:题目中提示,两个点之间存在多条trail ,将每条trail 存储.并记录有所trail中的最大值,作为上界.

二分查找,构图,将trail长度<=mid的边 构造双向的容量为1的边.计算最大流量 判断是否满足条件.

struct trail{
	int a,b,c;
};
trail tr[40001];

int trailnum=0;
void create_graph(int limit)
{
	dinic_init();
	for(int i=0;i<trailnum;i++)
	{
		int a = tr[i].a;
		int b = tr[i].b;
		int c = tr[i].c;
		if(c<=limit)
		{
			add_edge(a,b,1,1);
		}
	}
} 
int main() {
	
	int nnum,pnum,tnum;
	while(scanf("%d %d %d",&nnum,&pnum,&tnum)!=EOF)
	{
		trailnum = 0;
		int l = 0,r = -1;
		for(int i=0;i<pnum;i++)
		{
			int a,b,c;
			scanf("%d %d %d",&a,&b,&c);
			tr[trailnum].a = a;
			tr[trailnum].b = b;
			tr[trailnum++].c = c;
			if(c>r){
				r = c;
			}
		}
		int ans;
		while(l<=r)
		{
			int mid = (l+r)/2; 
			create_graph(mid);
		 	int flow = dinic(1,nnum);
		 	if(flow >= tnum)
		 	{
		 		ans = mid;
	 			r = mid-1;
	 		}
	 		else
	 		{
		 		l = mid+1;
		 	}
		}
		printf("%d\n",ans);
	}
点赞