解题报告 之 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
    评论
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1011 1012 1013 1014 1015 1017 1018 1019 1028 1032 1035 1040 1042 1045 1046 1047 1050 1056 1061 1062 1063 1065 1067 1068 1080 1083 1088 1089 1091 1094 1095 1102 1111 1113 1117 1118 1125 1126 1127 1129 1130 1131 1132 1141 1142 1144 1149 1151 1157 1159 1160 1163 1164 1166 1174 1177 1182 1183 1186 1188 1189 1190 1191 1195 1200 1201 1207 1218 1226 1251 1256 1258 1260 1273 1274 1276 1283 1298 1305 1306 1308 1315 1316 1319 1321 1323 1324 1325 1328 1338 1339 1364 1389 1401 1422 1423 1426 1455 1458 1459 1469 1477 1485 1511 1517 1519 1523 1552 1562 1564 1565 1573 1579 1651 1654 1655 1656 1658 1659 1663 1664 1699 1700 1703 1716 1730 1737 1740 1753 1771 1797 1799 1804 1833 1837 1840 1844 1861 1887 1906 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1970 1979 1980 1985 1988 1989 2000 2001 2002 2018 2019 2021 2027 2033 2044 2051 2081 2084 2104 2109 2112 2135 2136 2137 2153 2155 2181 2182 2184 2185 2186 2187 2188 2190 2192 2195 2228 2229 2234 2236 2241 2242 2245 2247 2248 2249 2253 2264 2287 2299 2301 2309 2336 2337 2348 2352 2353 2362 2371 2378 2386 2387 2388 2389 2392 2393 2394 2402 2403 2406 2411 2413 2419 2421 2446 2449 2456 2479 2488 2492 2503 2509 2513 2524 2528 2531 2533 2545 2553 2559 2564 2575 2576 2586 2591 2593 2594 2602 2623 2632 2656 2676 2680 2707 2750 2774 2777 2780 2782 2812 2818 2840 2908 2922 2934 2965 2983 2993 2996 3020 3041 3168 3169 3176 3183 3184 3185 3191 3193 3214 3219 3224 3250 3253 3255 3256 3257 3258 3259 3264 3267 3273 3275 3277 3278 3279 3280 3295 3297 3302 3303 3311 3312 3321 3325 3348 3349 3355 3356 3357 3368 3372 3386 3400 3421 3424 3425 3427 3428 3438 3452 3468 3486 3517 3561 3585 3589 3602 3612 3614 3615 3616 3617 3618 3619 3620 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3637 3660 3661 3662 3663 3664 3665 3666 3668 3669 3670 3671 3672 3673 3687
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值