POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)

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 = 305;
int k,c,m;
int mid,ylink[N][N],vy[N],check[N],d[N][N];

inline int dfs(int x){
    for(int i=1;i<=k;i++){
        if(!check[i]){
            if(d[x][i]<=mid) check[i]=1;
            else continue ;
            if(vy[i]<m){
                ylink[i][++vy[i]]=x;
                return 1;
            }
            for(int j=1;j<=vy[i];j++){
                int now = ylink[i][j];
                if(dfs(now)){
                    ylink[i][j]=x;
                    return 1;
                }
            }
        }
    }
    return 0;
}

inline int Check(int x){
    mem(vy);mem(ylink);
    for(int i=k+1;i<=k+c;i++){
        mem(check);
        if(!dfs(i)) return 0;
    }
    return 1;
}

int main(){
    scanf("%d%d%d",&k,&c,&m);
    for(int i=1;i<=k+c;i++) for(int j=1;j<=k+c;j++) scanf("%d",&d[i][j]);
    for(int i=1;i<=k+c;i++)
        for(int j=1;j<=k+c;j++)
            if(i!=j &&!d[i][j]) d[i][j]=INF;
    for(int t=1;t<=k+c;t++)for(int i=1;i<=k+c;i++)for(int j=1;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=1,r=INF+1,Ans;
    while(l<=r){
        mid=l+r>>1;
        if(Check(mid)){
            r=mid-1;
            Ans=mid;
        }else l=mid+1;
    }
    printf("%d\n",Ans);
    return 0;
}

原文地址:https://www.cnblogs.com/heyuhhh/p/9965401.html

时间: 2024-11-07 01:28:01

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

Optimal Milking(二分图多重匹配+二分)(网络流)

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

Jamie&#39;s Contact Groups(二分图多重匹配+二分)(网络流)

Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2289 Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list

POJ--2289--Jamie&#39;s Contact Groups【二分图多重匹配+二分答案】

链接:http://poj.org/problem?id=2289 题意:有n个人,m个分组,每个人可以分配到一些组别,问如何分能使得人数最多的组别人数最少. 思路:这道题二分+网络流也可以做,我这里是二分图多重匹配的做法.因为一个组别是一对多的关系,所以是多重匹配,我们二分多重匹配的限制,得到最小的限制可使二分图匹配,这个限制就是答案. 网上找的模板 #include<cstring> #include<string> #include<fstream> #inclu

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

Jamie's Contact Groups Time Limit: 15000/7000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 747    Accepted Submission(s): 303 Problem Description Jamie is a very popular girl and has quite a lot of friends, so she

Steady Cow Assignment(二分图多重匹配+二分)(网络流)

Steady Cow Assignment Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3189 Description Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of cou

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

题目大意: 有k个挤奶器,在牧场里有c头奶牛,每个挤奶器可以满足m个奶牛,奶牛和挤奶器都可以看成是实体,现在给出两个实体之间的距离,如果没有路径相连,则为0,现在问你在所有方案里面,这c头奶牛需要走的最大距离的最小值. 分析: 先将题目给出来的距离矩阵跑一下 Floyd 求出全源最短路方便后面建图, 这里注意一下除了对角线的点若有其他点为 0 则应将其值设置为 INF 代表不可达 在使用最大流判断是否存在解的时候,要对每个解都重新建图. 建图需要一个超级源点,把所有的奶牛与源点相连,容量设置为1

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

二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同的顶点集(i in A,j in B),则称图G为一个二分图. 1.一个二分图中的最大匹配数等于这个图中的最小点覆盖数 König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数.如果你还不知道什么是最小点覆盖,我也在这里说一下:假如选

poj2112 Optimal Milking --- 最大流,二分

nx个挤奶器,ny头奶牛,每个挤奶器最多能供m头奶牛使用. 现给出nx+ny之间的距离矩阵,求使得全部奶牛都到某个挤奶器挤奶所走的路程中,单个奶牛所走的最大路程的最小值. 开始感觉这个类似二分图匹配,不同之处在于挤奶器可以连接m个以内的奶牛,用网络流的模型是可以求出满足条件的解的. 问题是如何满足最大路程的最小值,这一种典型的二分的问法.. 所以我们二分答案,也就是枚举最大路程,直到求得最小值. 每次建边既添加所有最大路程以内的边,添加源点向每个挤奶器建边,容量为m,其他边都是1, 若返回的最大

Poj 2289 Jamie&#39;s Contact Groups (二分+二分图多重匹配)

题目链接: Poj 2289 Jamie's Contact Groups 题目描述: 给出n个人的名单和每个人可以被分到的组,问将n个人分到m个组内,并且人数最多的组人数要尽量少,问人数最多的组有多少人? 解题思路: 二分图多重匹配相对于二分匹配来说不再是节点间的一一对应,而是Xi可以对应多个Yi.所以我们就需要一个限制(Xi最多匹配几个Yi).当Yi需要匹配Xi的时候,Xi的匹配未到上限,直接匹配,否则进行增广路.其实是二分图多重匹配的模板题,再套一个二分枚举最多组的人数就OK咯.下面就上板