解题报告 之 POJ2455 Secret Milking Machine

解题报告 之 POJ2455 Secret Milking Machine


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 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.

Input

* 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.

Output

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

Sample Input

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

Sample Output

5

Hint

Farmer John can travel trails 1 - 2 - 3 - 7 and 1 - 6 - 7. None of the trails travelled exceeds 5 units in length. It is impossible for Farmer John to travel from 1 to 7 twice without using at least one trail of length 5. 

Huge input data,scanf is recommended.


题目大意:有n个节点,有p条路,第i条路的长度为di。有一个人要进行t次从节点1走到节点N的活动,且在所有t次活动中加起来,每条路总共最多只能走一次。现在要使得所有路途中走到的最长的一条路尽量短,问最短能有多短?


分析:此题仍然是看到最大值求最小,二分再加上最大流搞定。首先二分最大值mid,假设mid是可以满足条件的值,如果mid不能满足条件,则向上二分;如果mid满足条件,则向下二分。那么怎么判断一个mid是否满足条件呢?就是最大流了。首先,我们将图的每条边读入进来;然后扫一下(感觉可以排个序,后面会比较快),那么所有dis<mid的边即为可行边,那么连一条边到流量图中,权值为1。注意到原图和流量图是两个图,不要搞混了,权值是1而不是dis,表示一条路最多走一次,这与dis是没关系的。


然后求1到n节点的最大流,如果max_flow<t,则说明不能满足走t次的要求,mid要放宽(即向上二分)。否则说明可能还有更优解,向下二分。


火爆爆热腾腾的代码出炉啦:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std;

const int MAXN = 300;
const int MAXM = 90000;
const int INF = 0x3f3f3f3f;
struct Edge
{
	int to, next, cap;
};

struct rawEdge
{
	int from, to, dis;
};

Edge edge[MAXM];
rawEdge rawedge[MAXM];
int head[MAXN], level[MAXN];
int dis[250][250];
int src, des,cnt;


void addedge(int from,int to,int cap)
{
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;

	edge[cnt].to = from;
	edge[cnt].cap = cap;
	edge[cnt].next = head[to];
	head[to] = cnt++;
}

int bfs()
{
	queue<int> q;
	while (!q.empty())
		q.pop();
	memset(level, -1, sizeof level);
	level[src] = 0;
	q.push(src);

	while (!q.empty())
	{
		int u = q.front();
		q.pop();

		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap&&level[v] == -1)
			{
				level[v] = level[u] + 1;
				q.push(v);
			}
		}
	}
	return level[des] != -1;
}

int dfs(int u,int f)
{
	if (u == des) return f;
	int tem;
	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].cap > 0 && level[v] == level[u] + 1)
		{
			tem = dfs(v, min(f, edge[i].cap));
			if (tem > 0)
			{
				edge[i].cap -= tem;
				edge[i ^ 1].cap += tem;
				return tem;
			}
		}
	}
	level[u] = -1;
	return 0;
}

int Dinic()
{
	int ans = 0, tem;
	
	while (bfs())
	{
		while (tem=dfs(src, INF))
		{
			ans += tem;
		}
	}
	return ans;
}

int main()
{

	int n, m, t;
	int low, high, mid;
	while (scanf("%d%d%d",&n,&m,&t)==3)
	{
		
		low = INF;
		high = 0;
		src = 1;
		des = n;
		for (int i = 0; i < m; i++)
		{
			int f, t, d;
			scanf("%d%d%d", &f, &t, &d);
			rawedge[i].from = f;
			rawedge[i].to = t;
			rawedge[i].dis = d;
			low = min(low, d);
			high = max(high, d);
		}

		while (low < high)
		{
			mid = ( low + high) / 2;
			memset(head, -1, sizeof(head));
			cnt = 0;
			for (int i = 0; i <m; i++)
			{
				if (rawedge[i].dis <= mid)
					addedge(rawedge[i].from, rawedge[i].to, 1);
			}
			
			if (Dinic() < t) low = mid + 1;
			else high = mid;
		}
		printf("%d\n", low);
	}
	return 0;
}

看此题画风应该和之前的 POJ2112 Optimal Milking 是出自同一位大神之手,但不知道为何一直有恋牛情结。好吧,做了一些最大流之后发现主要是要先理解一个最大流模板,不然敲得时候容易抄错(校赛血淋淋的教训)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值