POJ2112 Optimal Milking (网络流dinic算法、二分法、Floyed算法)

15 篇文章 0 订阅
3 篇文章 0 订阅

Optimal Milking

Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 18898 Accepted: 6757
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

题意

FJ有K个挤奶器和C头奶牛,每头奶牛都需要挤奶,每个挤奶器最多可供M头奶牛挤奶。挤奶器编号为1~K,奶牛编号为K+1~K+C,给出相互之间的距离。
求C头奶牛需要走的最大距离的最小值。

分析

此题最大流表示在最大距离为x的情况下,能够完成挤奶的奶牛的最大数量maxFlow。
如果最大数量maxFlow大于等于奶牛数量C,那么距离x是符合题意的。
因此,二分搜索x即可得出正解。

流程

(1)用Floyed算法求出相互之间的最短距离
(2)二分最大距离x求解答案
(2.1)构图
(2.1.1)源点到各个挤奶器连一条容量为M的边
(2.1.2)各个奶牛到汇点连一条容量为1的边
(2.1.3)如果挤奶器与奶牛的距离小于等于x,连一条容量为1的边
(2.2)dinic算法求解最大流
(3)输出答案

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f

using namespace std;

const int maxe=1e4+10;
const int maxv=300;
struct edge
{
    int to,next,w;
}e[maxe<<1];
int head[maxv<<1],cnt;
int dis[260][260],depth[300];
int K,C,M;

void init()
{
    memset(head,-1,sizeof(head));
    cnt=-1;
}

void add_edge(int u,int v,int w)
{
    e[++cnt].to=v;
    e[cnt].w=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}
void _add(int u,int v,int w)
{
    add_edge(u,v,w);
    add_edge(v,u,0);
}

void floyed()
{
    for(int k=1;k<=K+C;k++)
        for(int i=1;i<=K+C;i++)
        for(int j=1;j<=K+C;j++)
        dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
void getMap()
{
    init();
    for(int i=1;i<=K;i++)
        _add(0,i,M);
    for(int i=K+1;i<=K+C;i++)
        _add(i,K+C+1,1);
}
bool bfs()
{
    memset(depth,0,sizeof(depth));
    queue<int> que;
    while(!que.empty()) que.pop();
    depth[0]=1;
    que.push(0);

    while(!que.empty())
    {
        int u=que.front();que.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            if(!depth[v] && e[i].w>0)
            {
                depth[v]=depth[u]+1;
                que.push(v);
            }
        }
    }

    return depth[K+C+1]>0;
}
int dfs(int u,int flow)
{
    if(u==K+C+1) return flow;

    int ret=0;
    for(int i=head[u];i!=-1&&flow;i=e[i].next)
    {
        int v=e[i].to;
        if(e[i].w>0 && depth[u]+1==depth[v])
        {
            int tmp=dfs(v,min(flow,e[i].w));
            if(tmp>0)
            {
                flow-=tmp;
                e[i].w-=tmp;
                e[i^1].w+=tmp;
                ret+=tmp;
            }
        }
    }
    return ret;
}
int dinic()
{
    int ans=0;
    while(bfs())
    {
        ans+=dfs(0,INF);
    }
    return ans;
}
bool ok(int x)
{
    getMap();
    for(int i=1;i<=K;i++)
    {
        for(int j=K+1;j<=K+C;j++)
        {
            if(dis[i][j]<=x)
                _add(i,j,1);
        }
    }
    return dinic()>=C;
}
void solve()
{
    int l=0,r=10000;
    while(l<r)
    {
        int mid=(l+r)/2;
        if(ok(mid))
            r=mid;
        else
            l=mid+1;
    }
    printf("%d\n",r);
}
int main()
{
    while(~scanf("%d%d%d",&K,&C,&M))
    {
        memset(dis,0,sizeof(dis));
        for(int i=1;i<=K+C;i++)
            for(int j=1;j<=K+C;j++)
            {
                scanf("%d",&dis[i][j]);
                if(!dis[i][j])
                    dis[i][j]=INF;
            }

        floyed();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值