POJ_2112_Optimal Milking(二分图多重匹配)

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

题意:k个挤奶器,c头奶牛,每个挤奶器可供k头奶牛使用,k+c的矩阵给出挤奶器和奶牛到彼此的距离,前k行是挤奶器,k+1到k+c行是奶牛。求在保证每头牛都能挤到奶的情况下,求牛的最小移动距离。

思路:floy跑一遍最短路,然后二分枚举+二分多重匹配找答案。

#include<algorithm>
#include<string.h>
#include<stdio.h>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,k;
int g[300][300],mpp[300][300];
int link[35][20],sum[35],used[35];
void floy()
{
    for(int k=1; k<=n+m; k++)
    {
        for(int i=1; i<=n+m; i++)
        {
            for(int j=1; j<=n+m; j++)
            {
                if(g[i][j]>g[i][k]+g[k][j])
                    g[i][j]=g[i][k]+g[k][j];
            }
        }
    }
}
int found(int u)
{
    for(int i=1; i<=n; i++)
    {
        if(mpp[u][i]&&!used[i])
        {
            used[i]=1;
            if(link[i][0]<sum[i])
            {
                link[i][++link[i][0]]=u;
                return true;
            }
            for(int j=1; j<=sum[i]; j++)
            {
                if(found(link[i][j]))
                {
                    link[i][j]=u;
                    return true;
                }
            }
        }
    }
    return false;
}
int hun()
{
    int ans=0;
    for(int i=1; i<=n; i++)
        link[i][0]=0;
    for(int i=1; i<=n; i++)
        sum[i]=k;
    for(int i=n+1; i<=n+m; i++)
    {
        memset(used,0,sizeof(used));
        if(found(i)) ans++;
    }
    return ans==m;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(int i=1; i<=n+m; i++)
        {
            for(int j=1; j<=n+m; j++)
            {
                scanf("%d",&g[i][j]);
                if(g[i][j]==0)
                    g[i][j]=inf;
            }
        }
        for(int i=1; i<=n+m; i++)
            g[i][i]=0;
        floy();
        int l=1,r=inf,mid;//题意明明是200是最大距离,可是这里换到210还是错。
                          //r还是得开到inf......
        while(l<=r)
        {
            memset(mpp,0,sizeof(mpp));
            mid=(l+r)>>1;
            for(int i=n+1; i<=n+m; i++)
            {
                for(int j=1; j<=n; j++)
                {
                    if(g[i][j]<=mid)
                        mpp[i][j]=1;
                }
            }
            if(hun())
                r=mid-1;
            else
                l=mid+1;
        }
        printf("%d\n",l);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/81364623