poj 2112 Optimal Milking (二分图匹配的多重匹配)
Description
FJ has moved his K ( <= K <= ) milking machines out into the cow pastures among the C ( <= C <= ) 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 ..K; the cow locations are named by ID numbers K+..K+C. Each milking point can "process" at most M ( <= M <= ) 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 : A single line with three space-separated integers: K, C, and M. * Lines .. ...: Each of these K+C lines of K+C space-separated integers describes the distances between pairs of various entities. The input forms a symmetric matrix. Line tells the distances from milking machine to each of the other entities; line tells the distances from machine to each of the other entities, and so on. Distances of entities directly connected by a path are positive integers no larger than . Entities not directly connected by a path have a distance of . The distance from an entity to itself (i.e., all numbers on the diagonal) is also given as . To keep the input lines of reasonable length, when K+C > , a row is broken into successive lines of 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
Sample Output
Source
题意:K个产奶机,C头奶牛,每个产奶机最多可供M头奶牛使用;并告诉了产奶机、奶牛之间的两两距离Dij(0<=i,j<K+C)。
问题:如何安排使得在任何一头奶牛都有自己产奶机的条件下,奶牛到产奶机的最远距离最短?最短是多少?
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 206
#define inf 1<<29
int k,c,m;
int mp[][];
int path[N][];
int match[];
int vis[];
void flyod(){
for(int L=;L<=k+c;L++){
for(int i=;i<=k+c;i++){
for(int j=;j<=k+c;j++){
if(mp[i][j]>mp[i][L]+mp[L][j]){
mp[i][j]=mp[i][L]+mp[L][j];
}
}
}
}
}
void changePath(int mid){
for(int i=;i<=c;i++){
for(int j=;j<=k;j++){
if(mp[k+i][j]<=mid){
for(int t=;t<=m;t++){
path[i][(j-)*m+t]=;
}
}
}
}
}
bool dfs(int x){
for(int i=;i<=k;i++){
for(int j=;j<=m;j++){
int u=(i-)*m+j;
if(path[x][u] && !vis[u]){
vis[u]=;
if(match[u]==- || dfs(match[u])){
match[u]=x;
return true;
}
}
}
}
return false;
}
bool judge(){ memset(match,-,sizeof(match));
for(int i=;i<=c;i++){
memset(vis,,sizeof(vis));
if(!dfs(i)){
return false;
}
}
return true; }
void solve(){
int L=,R=;
while(L<R){
int mid=(L+R)>>;
memset(path,,sizeof(path));
changePath(mid);
if(judge()){
R=mid;
}else{
L=mid+;
}
}
printf("%d\n",L);
}
int main()
{
while(scanf("%d%d%d",&k,&c,&m)==){ for(int i=;i<=k+c;i++){
for(int j=;j<=k+c;j++){
scanf("%d",&mp[i][j]);
if(mp[i][j]==){
mp[i][j]=inf;
}
}
} flyod();
solve();
}
return ;
}
附上有注释的代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> const int MAXK = + ;
const int MAXC = + ;
const int MAXM = + ;
const int INF = ; using namespace std; int k, c, m;
int map[MAXK+MAXC][MAXK+MAXC];
bool path[MAXC][MAXK*MAXM];
int match[MAXK*MAXM];
bool vst[MAXK*MAXM]; /* 把每个挤奶器点分裂成 m 个点,选边权 <=tmp 的边建立二分图 */
void buildGraph(int tmp)
{
memset(path, false, sizeof(path)); for (int i=; i<=c; i++)
for (int j=; j<=k; j++)
if (map[k+i][j] <= tmp)
{
for (int t=; t<=m; t++)
{
path[i][(j-)*m+t] = true;
}
}
} bool DFS(int i)
{
for (int j=; j<=k*m; j++)
{
if (path[i][j] && !vst[j])
{
vst[j] = true;
if (match[j] == - || DFS(match[j]))
{
match[j] = i;
return true;
}
}
}
return false;
} /* 针对该题,做了小小的修改,全部匹配返回 true, 否则返回 false */
bool maxMatch()
{
memset(match, -, sizeof(match));
for (int i=; i<=c; i++)
{
memset(vst, false, sizeof(vst));
if (!DFS(i))
return false;
}
return true;
} /* 二分答案,求二分图最大匹配 */
void solve()
{
int low = , high = *(k+c), mid;
while (low < high)
{
mid = (low + high)/;
buildGraph(mid);
maxMatch() == true ? high = mid : low = mid+;
}
printf("%d\n", low);
} void floyd()
{
int i, j, h, t = k+c;
for (h=; h<=t; h++)
for (i=; i<=t; i++)
for (j=; j<=t; j++)
if (map[i][j] > map[i][h]+map[h][j])
map[i][j] = map[i][h]+map[h][j];
} int main()
{
scanf("%d %d %d", &k, &c, &m);
for (int i=; i<=k+c; i++)
for (int j=; j<=k+c; j++)
{
scanf("%d", &map[i][j]);
if (map[i][j] == )
map[i][j] = INF;
}
floyd();
solve();
return ;
}
poj 2112 Optimal Milking (二分图匹配的多重匹配)的更多相关文章
- Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)
题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...
- POJ 2112 Optimal Milking (二分+最短路径+网络流)
POJ 2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS Memory Limit: 30000K To ...
- POJ 2112 Optimal Milking (二分 + floyd + 网络流)
POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...
- POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】
Optimal Milking Time Limit:2000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- POJ 2112 Optimal Milking (Floyd+二分+最大流)
[题意]有K台挤奶机,C头奶牛,在奶牛和机器间有一组长度不同的路,每台机器每天最多能为M头奶牛挤奶.现在要寻找一个方案,安排每头奶牛到某台机器挤奶,使得C头奶牛中走过的路径长度的和的最大值最小. 挺好 ...
- POJ 2112: Optimal Milking【二分,网络流】
题目大意:K台挤奶机,C个奶牛,每台挤奶器可以供M头牛使用,给出奶牛和和机器间的距离矩阵,求所有奶牛走最大距离的最小值 思路:最大距离的最小值,明显提示二分,将最小距离二分之后问题转化成为:K台挤奶机 ...
- POJ 2112 Optimal Milking (二分 + 最大流)
题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...
- POJ 2112 Optimal Milking (Dinic + Floyd + 二分)
Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 19456 Accepted: 6947 ...
随机推荐
- acid数据库事务正确执行的四个基本要素的缩写编辑本义项
ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...
- android 分辨率自适应
1.术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸 摩托罗拉milestone手机是3.7英寸 A ...
- IE6下的怪异解析知识点补充
转载请注明出处:HTMl5自由者
- lua pbc
先要将proto文件编译成.pb文件,然后再动态绑定实现lua protobuffer,这就需要了解云风做的pbc的项目,地址为:https://github.com/cloudwu/pbc/blob ...
- Android文字的复制和粘贴
Android中提供了简单的额复制粘贴功能.代码很简单 复制文字的代码: ClipboardManager cbm= (ClipboardManager) MainActivity.this .get ...
- 什么是工程师文化?各位工程师是为什么活的?作为一个IT或互联网公司为什么要工程师文化?
为什么要工程师文化? 看看最近二十年来社会的发展,计算机和互联网已经渗透到了这个社会的每一个角落,各式各样的计算机技术成为了整个世界发展的强大引擎,各式各样的创新,无论是业务创新还是技术创新,都是依托 ...
- 免费SVN源代码在线托管
免费的SVN源代码在线托管网站很多,用的最多的是TaoCode吧.但是一般都要求开源,支持私有项目的普遍收费,要不就是流量很少,不够用.对比了一下,发现好库正好能满足需要. 网址: http://ww ...
- mybatis的简单使用
使用mybatis数据库时,需要添加一下jar包: asm-3.3.1.jarcglib-2.2.2.jarjavassist-3.17.1-GA.jarlog4j-1.2.17.jarmybatis ...
- 前端笔试题目总结——应用JavaScript函数递归打印数组到HTML页面上
数组如下: var item=[{ name:'Tom', age:70, child:[{ name:'Jerry', age:50, child:[{ name:'William', age:20 ...
- cocos2dx 帧动画(iOS)
植物大战僵尸的植物摇摆效果 //帧动画 Animation *animation = Animation::create(); Sprite *sprite = Sprite::create(&quo ...