【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流

题目描述

Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <= 200) trips to the machine during its construction. He has a secret tunnel that he uses only for the return trips. The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks. To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails. Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.) It is guaranteed that FJ can make all T trips without reusing a trail.

约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T≤200)次.他的农场里有秘密的地道,但约翰只在返回的时候用它.农场被划分成N(2≤N≤200)块区域,用1到200标号.这些区域被P(1≤P≤40000)条道路连接,每条路有一个小于10^6的长度L.两块区域之间可能有多条道路连接.为了减少被发现的可能,约翰不会两次经过农场上的任何一条道路.当然了,他希望走最短的路. 请帮助约翰寻找这T次从仓库走到挤奶机所在地的路线.仓库是区域1,挤奶机所在地是区域N.我们现在要求的是约翰经过的这些道路中最长的路的长度最小是多少,当然他不能重复走某一条路.请注意,我们要求的不是最短的总路程长度,而是所经过的直揍连接两个区域的道路中最长的道路的最小长度. 数据保证约翰可以找到T条没有重复的从仓库到挤奶机所在区域的路.

输入

* Line 1: Three space-separated integers: N, P, and T * Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.

第1行是3个整数N、P和T,用空格隔开.

第2到P+1行,每行包括3个整数,Ai,Bi,Li.表示区域Ai、Bi之间有一条长度为Li的道路.

输出

* Line 1: A single integer that is the minimum possible length of the longest segment of Farmer John‘s route.

输出只有一行,包含一个整数,即约翰经过的这些道路中最长的路的最小长度.

样例输入

7 9 2
1 2 2
2 3 5
3 7 5
1 4 1
4 3 1
4 5 7
5 7 1
1 6 3
6 7 3

样例输出

5



题解

二分+网络流最大流

显然最大长度满足二分性质,我们可以二分长度mid,这样只能选择长度小于等于mid的边来走。

然后由于题目限制了每条边只能走一次,要求1到n的路径数,显然这是一个最大流问题。

对于原图中的边x->y,长度为z,如果z<=mid,则连边x->y,容量为1;否则不连边。

然后跑最大流,判断是否大于等于T即可,并对应调整上下界。

说实话这道网络流真是再水不过了。

#include <cstdio>
#include <cstring>
#include <queue>
#define N 210
#define M 40010
using namespace std;
queue<int> q;
int n , m , p , x[M] , y[M] , z[M] , head[N] , to[M << 2] , val[M << 2] , next[M << 2] , cnt , s , t , dis[N];
void add(int x , int y , int z)
{
	to[++cnt] = y , val[cnt] = z , next[cnt] = head[x] , head[x] = cnt;
	to[++cnt] = x , val[cnt] = 0 , next[cnt] = head[y] , head[y] = cnt;
}
bool bfs()
{
	int x , i;
	memset(dis , 0 , sizeof(dis));
	while(!q.empty()) q.pop();
	dis[s] = 1 , q.push(s);
	while(!q.empty())
	{
		x = q.front() , q.pop();
		for(i = head[x] ; i ; i = next[i])
		{
			if(val[i] && !dis[to[i]])
			{
				dis[to[i]] = dis[x] + 1;
				if(to[i] == t) return 1;
				q.push(to[i]);
			}
		}
	}
	return 0;
}
int dinic(int x , int low)
{
	if(x == t) return low;
	int temp = low , i , k;
	for(i = head[x] ; i ; i = next[i])
	{
		if(val[i] && dis[to[i]] == dis[x] + 1)
		{
			k = dinic(to[i] , min(temp , val[i]));
			if(!k) dis[to[i]] = 0;
			val[i] -= k , val[i ^ 1] += k;
			if(!(temp -= k)) break;
		}
	}
	return low - temp;
}
bool judge(int mid)
{
	int i , ans = 0;
	memset(head , 0 , sizeof(head)) , cnt = 1;
	for(i = 1 ; i <= m ; i ++ )
		if(z[i] <= mid)
			add(x[i] , y[i] , 1) , add(y[i] , x[i] , 1);
	while(bfs()) ans += dinic(s , 0x7fffffff);
	return ans >= p;
}
int main()
{
	int i , l = 0 , r = 0 , mid , ans = -1;
	scanf("%d%d%d" , &n , &m , &p) , s = 1 , t = n;
	for(i = 1 ; i <= m ; i ++ ) scanf("%d%d%d" , &x[i] , &y[i] , &z[i]) , r = max(r , z[i]);
	while(l <= r)
	{
		mid = (l + r) >> 1;
		if(judge(mid)) ans = mid , r = mid - 1;
		else l = mid + 1;
	}
	printf("%d\n" , ans);
	return 0;
}
时间: 2024-08-10 23:20:01

【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流的相关文章

[bzoj1733][Usaco2005 feb]Secret Milking Machine 神秘的挤奶机_网络流

[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 题目大意:约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T≤200)次.他的农场里有秘密的地道,但约翰只在返回的时候用它.农场被划分成N(2≤N≤200)块区域,用1到200标号.这些区域被P(1≤P≤40000)条道路连接,每条路有一个小于10^6的长度L.两块区域之间可能有多条

BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机

Description 约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农场里,使它不被发现.在挤奶机制造的过程中,他需要去挤奶机所在的地方T(1≤T≤200)次.他的农场里有秘密的地道,但约翰只在返回的时候用它.农场被划分成N(2≤N≤200)块区域,用1到200标号.这些区域被P(1≤P≤40000)条道路连接,每条路有一个小于10^6的长度L.两块区域之间可能有多条道路连接.为了减少被发现的可能,约翰不会两次经过农场上的任何一条道路.当然了

[BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】

题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MaxN = 200 + 5,

POJ 2455 Secret Milking Machine(二分+最大流)

POJ 2455 Secret Milking Machine 题目链接 题意:一个无向图,要求有T条不重复道路可以从1走到t,问道路中最大边的最小值可以是多少 思路:二分+最大流,二分长度,连起边,注意是无向图,所以反向边是有容量的,然后源点和1连容量t,n和汇点连容量是t 代码: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespa

POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: 2859 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

poj 2455 Secret Milking Machine 【二分 + 最大流】 【1到N不重复路径不少于T条时,求被选中路径上的最大边权值 的最小值】

Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10620   Accepted: 3110 Description Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within

POJ2455 Secret Milking Machine【二分,最大流】

题目大意:N个点P条边,令存在T条从1到N的路径,求路径上的边权的最大值最小为多少 思路:做了好多二分+最大流的题了,思路很好出 二分出最大边权后建图,跑dinic 问题是....这题是卡常数的好题!!!!! T了8发以后实在受不了,瞄了眼网上的程序,齐刷刷的邻接矩阵....论邻接矩阵的优越性 但不信邪的我终于反复优化常数后邻接表A了 //TLE的程序 #include <stdio.h> #include <iostream> #include <string.h>

POJ 2455 Secret Milking Machine (二分 + 最大流)

题目大意: 给出一张无向图,找出T条从1..N的路径,互不重复,求走过的所有边中的最大值最小是多少. 算法讨论: 首先最大值最小就提醒我们用二分,每次二分一个最大值,然后重新构图,把那些边权符合要求的边加入新图,在新图上跑网络流. 这题有许多注意的地方: 1.因为是无向图,所以我们在加正向边和反向边的时候,流量都是1,而不是正向边是1,反向边是0. 2.题目中说这样的路径可能不止t条,所以我们在最后二分判定的时候不能写 == t,而是要 >= t. 3.这题很容易T ,表示我T了N遍,弱菜啊.

poj 2455 Secret Milking Machine

[题意]:(半天没懂题目什么意思,诶..)FJ有N块地,这些地之间有P条双向路,每条路的都有固定的长度l.现在要你找出从第1块地到第n块地的T条不同路 径,每条路径上的路不能与先前的路径重复,问这些路径中的最长路的最小是多少. [思路]二分判定最长路的最小值,再用小于这个值的边建图跑最大流.建图的话,整张无向图(记得是无向图! 对一条边正向方向都要添加wa 好久  )加一个源点和点1 连边容量无穷大,n点和会汇点连边,容量无穷大,最大流是多少能够找到的不同的路径就是多少. 1 #include<