码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-11-15 19:50:21      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:esc   fine   str   out   write   ext   process   mac   pac   

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;
}

 

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

标签:esc   fine   str   out   write   ext   process   mac   pac   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!