Optimal Milking-POJ - 2112(多重匹配+二分)

34 篇文章 0 订阅
31 篇文章 0 订阅

Optimal Milking-POJ - 2112(多重匹配+二分)

source:USACO 2003 U S Open
judge:vjudge
Time limit:2000 ms
Case time limit:1000 ms
Memory limit:30000 kB
OS:Linux

描述

FJ has moved his K ( 1 &lt; = K &lt; = 30 ) K (1 &lt;= K &lt;= 30) K(1<=K<=30) milking machines out into the cow pastures among the C ( 1 &lt; = C &lt; = 200 ) C (1 &lt;= C &lt;= 200) C(1<=C<=200) cows. A set of paths of various lengths runs among the cows and the milking machines. The milking machine locations are named by ID numbers 1.. K 1..K 1..K; the cow locations are named by ID numbers K + 1.. K + C K+1..K+C K+1..K+C.

Each milking point can “process” at most M ( 1 &lt; = M &lt; = 15 ) M (1 &lt;= M &lt;= 15) M(1<=M<=15) cows each day.

Write a program to find an assignment for each cow to some milking machine so that the distance the furthest-walking cow travels is minimized (and, of course, the milking machines are not overutilized). At least one legal assignment is possible for all input data sets. Cows can traverse several paths on the way to their milking machine.

Input

  • Line 1 1 1: A single line with three space-separated integers: K , C , K, C, K,C, and M M M.

  • Lines 2 2 2… …: Each of these K + C K+C K+C lines of K + C K+C K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line 2 2 2 tells the distances from milking machine 1 1 1 to each of the other entities; line 3 3 3 tells the distances from machine 2 2 2 to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than 200 200 200. Entities not directly connected by a path have a distance of 0 0 0. The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as 0. To keep the input lines of reasonable length, when K + C &gt; 15 K+C &gt; 15 K+C>15, a row is broken into successive lines of 15 15 15 numbers and a potentially shorter line to finish up a row. Each new row begins on its own line.

Output

A single line with a single integer that is the minimum possible total distance for the furthest walking cow.

Sample Input

2 3 2
0 3 2 1 1
3 0 3 2 0
2 3 0 1 0
1 2 1 0 2
1 0 0 2 0

Sample Output

2

题意

FJ把他的 K ( 1 &lt; = K &lt; = 30 ) K(1&lt;=K&lt;=30) K1<=K<=30挤奶机移到了 C ( 1 &lt; = C &lt; = 200 ) C(1&lt;=C&lt;=200) C1<=C<=200奶牛的牧场上。奶牛和挤奶机之间有一组不同长度的小路。挤奶机位置由ID号1…K命名;奶牛位置由ID号 K + 1.. K + C K+1..K+C K+1..K+C命名。

每个挤奶点每天最多可“加工” m ( 1 &lt; = m &lt; = 15 ) m(1&lt;=m&lt;=15) m1<=m<=15头奶牛。

编写一个程序,为每头奶牛找到一个分配给某台挤奶机的任务,这样就可以将最远的奶牛行走距离最小化(当然,挤奶机也不会被过度利用)。对于所有输入数据集,至少可以进行一次合法分配。奶牛在去挤奶机的路上可以穿过几条小路。

输入

*第1行:有三个空格分隔的整数的单行: k 、 c k、c kc m m m

*第2行……:这些k+c行中的每一行 k + c k+c k+c空间分隔整数描述了不同实体对之间的距离。输入形成一个对称矩阵。第2行表示从挤奶机1到其他每个实体的距离;第3行表示从机器2到其他每个实体的距离,依此类推。由路径直接连接的实体的距离是不大于200的正整数。不直接由路径连接的实体的距离为0。从一个实体到它自身的距离(即对角线上的所有数字)也被表示为0。为了保持输入行的长度合理,当 k + c &gt; 15 k+c&gt;15 k+c>15时,一行被分成15个数字的连续行和一行可能更短的行来完成一行。每一新行都以自己的行开始。

输出

带有单个整数的单行,它是最远的奶牛可能的最小总距离。

思路

由于要把总距离尽可能小,所以需要走一波最短路。
看到最远的奶牛的最小距离就应该想到二分,利用多重匹配检验枚举值是否可行逼近极限值

代码

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <string.h>
#include <stack>
#include <queue>
#include <set>
#include <cmath>
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i<= (b); i++)
#define maxn 305
#define inf 0x3f3f3f3f
using namespace std;
int K, C, M;
int a[maxn][maxn];//邻接矩阵,数值为权值
int f[maxn][maxn];//f[i][j]表示编号为i的挤奶机所约定的奶牛编号
int cnt[maxn];//数值为每个挤奶机约定的奶牛的个数
int used[maxn];//dfs的标记变量
bool dfs(int u, int lim) {
	_for(i, K) {
		if (!used[i] && a[u][i] && a[u][i] <= lim) {
			used[i]++;
			if (cnt[i] < M) {//挤奶机还有空位置
				f[i][cnt[i]++] = u;
				return 1;
			}
			else {//挤奶机没有空位置了,但可以让其他奶牛腾出一个位置
				_for(j, cnt[i]) {
					if (dfs(f[i][j], lim)) {
						f[i][j] = u;
						return 1;
					}
				}
			}
		}
	}
	return 0;
}
int Maxmatch(int lim) {
	memset(f, 0, sizeof(f));
	memset(cnt, 0, sizeof(cnt));
	_rep(i, K, K + C - 1) {
		memset(used, 0, sizeof(used));
		if (!dfs(i, lim)) return 0;
	}
	return 1;
}
void Floyd() {
	_for(k, K + C) {
		_for(i, K + C) {
			_for(j, K + C) {
				if (a[i][k] + a[k][j] < a[i][j]) {
					a[i][j] = a[i][k] + a[k][j];
				}
			}
		}
	}
}
int main() {
	cin >> K >> C >> M;
	memset(a, 0x3f, sizeof(a));
	_for(i, K + C) {
		_for(j, K + C) {
			int x;
			cin >> x;
			if (x)	a[i][j] = a[j][i] = x;
		}
	}
	Floyd();
	int l = 1, r = maxn * 205, ans = r;
	while (r >= l) {
		int mid = (l + r) / 2;
		if (Maxmatch(mid)) {
			ans = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	cout << ans << "\n";
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值