poj2112 二分+最大流

本文介绍了一种算法,用于解决如何为每头牛分配最佳挤奶机的问题,以最小化最远行走距离,并确保挤奶机不过载。该算法通过构建图模型并使用改进的ISAP算法求解最大流问题来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Optimal Milking

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem 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
 

Source
PKU
 
和上一题基本一个思路额~~
本来想着能不能直接建边不用floyd 后来觉得不行啊~~
#include <cstring>
#include <cstdio>
#include <queue>
#define MAXN 5000
#define MAXM 500000
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
    int u,v,f,c;
};
node edge[MAXM*3];
int first[MAXN],next[MAXM*3];
int gap[MAXN],d[MAXN],curedge[MAXN],pre[MAXN];
int cc;
inline void add_edge(int u,int v,int f,int c)
{
    edge[cc].u=u;
    edge[cc].v=v;
    edge[cc].f=f;
    edge[cc].c=c;
    next[cc]=first[u];
    first[u]=cc;
    cc++;

    edge[cc].u=v;
    edge[cc].v=u;
    edge[cc].f=0;
    edge[cc].c=0;
    next[cc]=first[v];
    first[v]=cc;
    cc++;

}
int ISAP(int s,int t,int n)
{
    int cur_flow,flow_ans=0,u,tmp,neck,i,v;
    memset(d,0,sizeof(d));
    memset(gap,0,sizeof(gap));
    memset(pre,-1,sizeof(pre));
    for(i=0;i<=n;i++)
        curedge[i]=first[i];
    gap[0]=n+1;
    u=s;
    while(d[s]<=n)
    {
        if(u==t)
        {
            cur_flow=inf;
            for(i=s;i!=t;i=edge[curedge[i]].v)
            {
                if(cur_flow>edge[curedge[i]].f)
                {
                    neck=i;
                    cur_flow=edge[curedge[i]].f;
                }
            }
            for(i=s;i!=t;i=edge[curedge[i]].v)
            {
                tmp=curedge[i];
                edge[tmp].f-=cur_flow;
                edge[tmp^1].f+=cur_flow;
            }
            flow_ans+=cur_flow;
            u=neck;
        }
        for(i=curedge[u];i!=-1;i=next[i])
        {
            v=edge[i].v;
            if(edge[i].f&&d[u]==d[v]+1)
                break;
        }
        if(i!=-1)
        {
            curedge[u]=i;
            pre[v]=u;
            u=v;
        }
        else
        {
            if(0==--gap[d[u]])
                break;
            curedge[u]=first[u];
            for(tmp=n+5,i=first[u];i!=-1;i=next[i])
                if(edge[i].f)
                    tmp=min(tmp,d[edge[i].v]);
            d[u]=tmp+1;
            ++gap[d[u]];
            if(u!=s)
                u=pre[u];
        }
    }
    return flow_ans;
}
void build(int limit,int cnt,int m)
{
    int i;
    for(i=0;i<cnt;i=i+2)
    {
        edge[i].f=m;
        edge[i^1].f=0;
    }
    for(i=cnt;i<cc;i=i+2)
    {
        edge[i].f=(edge[i].c<=limit);
        edge[i^1].f=0;
    }
}
int dist[300][300];
void Floyd(int n)
{
    int i,j,k;
    for(k=1;k<=n;k++)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(dist[i][k]!=inf&&dist[k][j]!=inf&&
                   dist[i][j]>dist[i][k]+dist[k][j])
                {
                    dist[i][j]=dist[i][k]+dist[k][j];
                }
            }
        }
    }
}
int main()
{
    int k,c,m;
    while(scanf("%d%d%d",&k,&c,&m)!=EOF)
    {
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        cc=0;
        int i,j;
        int s=0,t=k+c+1;
        for(i=1;i<=k;i++)
            add_edge(i,t,m,0);
        int cnt1=cc;
        for(i=1;i<=k+c;i++)
        {
            for(j=1;j<=k+c;j++)
            {
                scanf("%d",&dist[i][j]);
                if(dist[i][j]==0)
                    dist[i][j]=inf;
            }
        }
        Floyd(k+c);
        for(i=k+1;i<=k+c;i++)
        {
            for(j=1;j<=k;j++)
                add_edge(i,j,inf,dist[i][j]);

        }
        //int cnt2=cc;
        for(i=k+1;i<=k+c;i++)
            add_edge(s,i,1,0);

        int l=0,r=400000;
        int mid,ans=inf;
        while(l<=r)
        {
            mid=(l+r)>>1;
            build(mid,cnt1,m);
            int res=ISAP(s,t,t);
            if(res<c)
                l=mid+1;
            else if(res>=c)
            {
                ans=min(ans,mid);
                r=mid-1;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值