poj 2112 Optimal Milking 二分图多重匹配+floyd+二分

题目:

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

思路:

先将各个点的最短路径求出来。

然后建立牛和机器的图。

然后根据二分的值来建另一张图,然后跑多重匹配。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=250;
const int INF=0x3f3f3f3f;
int a[maxn][maxn];
int head[maxn];
int k,c,m;
int match[maxn][maxn];
int num[maxn];
int vis[maxn];
int ma[maxn][maxn];
struct edge
{
    int len;
    int to;
    int next;
}edge[maxn*maxn];
void add (int id,int u,int v,int len)
{
    edge[id].to=v;
    edge[id].next=head[u];
    edge[id].len=len;
    head[u]=id;
}
bool Find(int x,int mid)
{
    for (int i=1;i<=k;i++)
    {
        if(!vis[i]&&ma[x][i])
        {
            vis[i]=1;
            if(num[i]<m)
            {
                match[i][num[i]++]=x;
                return true;
            }
            for (int j=0;j<num[i];j++)
            {
                if(Find(match[i][j],mid))
                {
                    match[i][j]=x;
                    return true;
                }
            }
        }
    }
    return false;
}
bool algor(int mid)
{
    memset (num,0,sizeof(num));
    for (int i=1;i<=c;i++)
    {
        memset (vis,0,sizeof(vis));
        if(!Find(i,mid)) return false;
    }
    return true;
}
int main()
{
    while(scanf("%d%d%d",&k,&c,&m)!=EOF)
    {
        int cnt=0;
        memset (head,-1,sizeof(head));
        for (int i=1;i<=k+c;i++)
        {
            for (int j=1;j<=k+c;j++)
            {
                scanf("%d",&a[i][j]);
                if(a[i][j]==0) a[i][j]=INF;
            }
        }
        for (int i=1;i<=c;i++)
        {
            for (int j=1;j<=k;j++)
            {
                add(++cnt,i,j,a[i+k][j]);
            }
        }
        for (int m=1;m<=k+c;m++)
        {
            for (int i=1;i<=k+c;i++)
            {
                for (int j=1;j<=k+c;j++)
                {
                    if(a[i][j]>a[i][m]+a[m][j])
                    {
                        a[i][j]=a[i][m]+a[m][j];
                    }
                }
            }
        }
        int l=0,r=INF;
        while(l<r)
        {
            memset (ma,0,sizeof(ma));
            int mid=(l+r)/2;
            for (int i=1;i<=c;i++)
            {
                for (int j=1;j<=k;j++)
                {
                    if(a[i+k][j]<=mid) ma[i][j]=1;
                }
            }
            if(algor(mid))
            {
                r=mid;
            }
            else
            {
                l=mid+1;
            }
        }
        printf("%d\n",r);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值