Optimal Milking

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 20262   Accepted: 7230
Case Time Limit: 1000MS

Description:

FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the 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; the cow locations are named by ID numbers K+1..K+C.

Each milking point can "process" at most 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: A single line with three space-separated integers: K, C, and M.

* Lines 2.. ...: 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 2 tells the distances from milking machine 1 to each of the other entities; line 3 tells the distances from machine 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. Entities not directly connected by a path have a distance of 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 > 15, a row is broken into successive lines of 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

题意:

C只奶牛,K个奶牛机,现在每只奶牛都要走到一个奶牛机,但是每个奶牛机只能容纳一定数量的奶牛,问奶牛到达奶牛机最远路径的最小值是多少。

题解:

这题可以建模为二分图多重匹配,并且根据题目要求,我们可以想到二分最远距离。但是此题需要注意的是,题目中给出的距离是直接的点与点之间的距离,但是奶牛走到奶牛机并不一定只有一条路径。

所以我们可以通过Floyd预处理一下(有点贪心的思想),求出两点间的最短距离。

如果没有通过Floyd预处理,那么最后二分出来的值会有偏差。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mem(x) memset(x,0,sizeof(x))
#define INF 10000000
using namespace std; const int N = ;
int k,c,m;
int mid,ylink[N][N],vy[N],check[N],d[N][N]; inline int dfs(int x){
for(int i=;i<=k;i++){
if(!check[i]){
if(d[x][i]<=mid) check[i]=;
else continue ;
if(vy[i]<m){
ylink[i][++vy[i]]=x;
return ;
}
for(int j=;j<=vy[i];j++){
int now = ylink[i][j];
if(dfs(now)){
ylink[i][j]=x;
return ;
}
}
}
}
return ;
} inline int Check(int x){
mem(vy);mem(ylink);
for(int i=k+;i<=k+c;i++){
mem(check);
if(!dfs(i)) return ;
}
return ;
} 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",&d[i][j]);
for(int i=;i<=k+c;i++)
for(int j=;j<=k+c;j++)
if(i!=j &&!d[i][j]) d[i][j]=INF;
for(int t=;t<=k+c;t++)for(int i=;i<=k+c;i++)for(int j=;j<=k+c;j++)
if(d[i][j]>d[i][t]+d[t][j]) d[i][j]=d[i][t]+d[t][j];
int l=,r=INF+,Ans;
while(l<=r){
mid=l+r>>;
if(Check(mid)){
r=mid-;
Ans=mid;
}else l=mid+;
}
printf("%d\n",Ans);
return ;
}

POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)的更多相关文章

  1. 稳定的奶牛分配 && 二分图多重匹配+二分答案

    题意: 农夫约翰有N(1<=N<=1000)只奶牛,每只奶牛住在B(1<=B<=20)个奶牛棚中的一个.当然,奶牛棚的容量有限.有些奶牛对它现在住的奶牛棚很满意,有些就不太满意 ...

  2. HDU 1669 二分图多重匹配+二分

    Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/ ...

  3. POJ-2112 Optimal Milking(floyd+最大流+二分)

    题目大意: 有k个挤奶器,在牧场里有c头奶牛,每个挤奶器可以满足m个奶牛,奶牛和挤奶器都可以看成是实体,现在给出两个实体之间的距离,如果没有路径相连,则为0,现在问你在所有方案里面,这c头奶牛需要走的 ...

  4. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  5. kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

    二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...

  6. POJ 2112 Optimal Milking (Floyd+二分+最大流)

    [题意]有K台挤奶机,C头奶牛,在奶牛和机器间有一组长度不同的路,每台机器每天最多能为M头奶牛挤奶.现在要寻找一个方案,安排每头奶牛到某台机器挤奶,使得C头奶牛中走过的路径长度的和的最大值最小. 挺好 ...

  7. poj 2289 Jamie's Contact Groups【二分+最大流】【二分图多重匹配问题】

    题目链接:http://poj.org/problem?id=2289 Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K ...

  8. POJ3189:Steady Cow Assignment(二分+二分图多重匹配)

    Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7482   Accepted: ...

  9. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

随机推荐

  1. python中一些内置函数实例

    lambda表达式 简单函数可用lambda表达式 1. def f1() return(123) r1=f1() print() 2. f2=lambda:123 r2=f2() print() 以 ...

  2. PAT (Basic Level) Practice (中文)1002

    1002 写出这个数 (20 分) 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1 ...

  3. (数据科学学习手札34)多层感知机原理详解&Python与R实现

    一.简介 机器学习分为很多个领域,其中的连接主义指的就是以神经元(neuron)为基本结构的各式各样的神经网络,规范的定义是:由具有适应性的简单单元组成的广泛并行互连的网络,它的组织能够模拟生物神经系 ...

  4. ORA-12705: Cannot access NLS data files or invalid

    RedHat7.1 Oracle11gr2 oracle 默认的编码方式如下:SQL> select userenv('language') from dual; USERENV('LANGUA ...

  5. vs13发布web程序 iis上

    一.配置iis 1,找到控制面板--程序--启用或关闭Windows功能 2,从列表中选择Internet Infomation Services,并且把相应的功能条目勾选上,如果不清楚,可以全部选中 ...

  6. Hadoop入门案列,初学者Coder

    1.WordCount Job类: package com.simope.mr.wcFor; import org.apache.hadoop.conf.Configuration; import o ...

  7. NoSQL简单学习(一)

    只是简单的知道有这个东西,却从来没有去接触,今天看了几篇文章,记录一下,开始慢慢接触这一领域 简介: 8种Nosql数据库系统对比 http://blog.jobbole.com/1344/ 一网打尽 ...

  8. 【BZOJ 1269】文本编辑器

    题目 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对"文本编辑器"做了一个抽象的定义: Move k:将 ...

  9. hdu1506 Largest Rectangle in a Histogram

    Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...

  10. C++学习003-#define 自定义宏

    C++中可以用#define来定义自定义的宏 也可以用使用#define来定义常量 但是#define只是简单的替换,在定义常量的时候没有语法检测 所以在C++定义常量可以使用  Const修饰 #d ...