poj2112Optimal Milking【floyd最短路+二分图多重匹配+二分】

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

不是所有套用网络流模板的都叫网络流==

题意:已知k个挤奶机器和c个奶牛,已知所有点到所有点的距离,可以选择最短的方法(就是先floyd预处理一下)然后问你最短的最长边是多少==

二分加边的方法和

nefu500网购【二分+网络流】
一样,二分上限==

两个让我wa了很多遍的坑:

1 奶牛的个数是200!!

2 矩阵等于0的边是不可达的!!

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int k,c,m;
int dist[250][250];
void floyd()
{
    int n=k+c;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            for(int q=1;q<=n;q++)
            {
                if(dist[j][i]+dist[i][q]<dist[j][q])
                    {
                        dist[j][q]=dist[j][i]+dist[i][q];
                     //   printf("j=%d,i=%d,q=%d,distji=%d,distiq=%d,distjq=%d\n",j,i,q,dist[j][i],dist[i][q],dist[j][q]);
                    }
            }
        }
    }
}
const int mm=555555;
const int mn=555;
const int oo=1000000000;
int node,src,dest,edge;
int reach[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    reach[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    reach[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=reach[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while((delta=Dinic_dfs(src,oo)))ret+=delta;
    }
    return ret;
}
int main()
{
 //   freopen("cin.txt","r",stdin);
    while(~scanf("%d%d%d",&k,&c,&m))
    {
        for(int i=1;i<=k+c;i++)
            for(int j=1;j<=k+c;j++)
            {
                scanf("%d",&dist[i][j]);
                if(dist[i][j]==0)dist[i][j]=60000;
            }

        floyd();

        int l=1,r=60000,mid,ans=r;
        while(l<=r)
        {
            mid=(l+r)/2;
            prepare(2+k+c,0,k+c+1);
            for(int i=1;i<=k;i++)
            {
                addedge(0,i,m);
                for(int j=k+1;j<=k+c;j++)
                {
                    if(mid>=dist[i][j])addedge(i,j,1);
                }
            }
            for(int j=k+1;j<=k+c;j++)addedge(j,dest,1);
            int tmp=Dinic_flow();
        //    printf("tmp=%d\n",tmp);
            if(c==tmp) {r=mid-1;ans=mid;}
            else l=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值