poj2112 Optimal Milking (Dinic+Floyd+二分)

Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 9624 Accepted: 3474
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
 
    思路:首先用floyd求出奶牛和挤奶器之间的最短距离,然后二分枚举最长距离(maxdist),若奶牛和挤奶器之间的距离小于或等于maxdist,则建一条边,边容量为1;对于每一头奶牛i,建一条边s->i,边容量为1;对于每一台挤奶器i,建一条边i->t,边容量为M。
 
#include<iostream>
#include<cstdio>

using namespace std;
const int MAXN=300;
const int INF=(1<<29);
int dis[MAXN][MAXN];//任意两点的最短距离
int flow[MAXN][MAXN];//容量限制
int level[MAXN];//分层
int K,C,M;
int n;

void floyd()
{
    int i,j,k;
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            for(k=1;k<=n;k++)
                dis[j][k]=min(dis[j][i]+dis[i][k],dis[j][k]);
}

void build(int max)
{
    int i,j;
    for(i=0;i<=n+1;i++)
        for(j=0;j<=n+1;j++)
            flow[i][j]=flow[j][i]=0;
    for(i=K+1;i<=n;i++)
        flow[0][i]=1;
    for(i=1;i<=K;i++)
        flow[i][n+1]=M;
    for(i=K+1;i<=n;i++)
        for(j=1;j<=K;j++)
            if(dis[i][j]<=max)
                flow[i][j]=1;
}

int bfs()
{
    int queue[MAXN],front,rear;
    for(int i=0;i<=n+1;i++)
        level[i]=0;
    level[0]=1;
    front=rear=0;
    queue[rear++]=0;
    while(front!=rear)
    {
        int v=queue[front++];
        for(int i=0;i<=n+1;i++)
            if(!level[i]&&flow[v][i])
            {
                level[i]=level[v]+1;
                queue[rear++]=i;
            }
    }
    return level[n+1];
}

int find(int u,int f)
{
    if(u==n+1)
        return f;
    int t,sum;
    sum=0;
    for(int i=0;i<=n+1;i++)
    {
        if(f&&flow[u][i]&&level[u]+1==level[i])
        {
             int t=find(i,min(f,flow[u][i]));
             flow[u][i]-=t;
             flow[i][u]+=t;
             f-=t;
             sum+=t;
        }
    }
    return sum;
}

int dinic()
{
    int maxflow;
    int low,mid,up;
    low=0;
    up=100000;
    while(low<=up)
    {
        mid=(low+up)>>1;
        build(mid);
        maxflow=0;
        while(bfs())
            maxflow+=find(0,INF);
        if(maxflow<C)
            low=mid+1;
        else
            up=mid-1;
    }
    return up+1;
}

int main()
{
    int i,j;
    while(~scanf("%d%d%d",&K,&C,&M))
    {
        n=K+C;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                scanf("%d",&dis[i][j]);
                if(dis[i][j]==0)
                    dis[i][j]=INF;
            }
        floyd();
        printf("%d\n",dinic());
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值