poj 2112 Optimal Milking(floyd+二分+最大流)

Language:
Optimal Milking
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 12782 Accepted: 4612
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


题意就是  有k个挤奶器  有c头奶牛  每个挤奶器最多给m头奶牛挤奶  

挤奶器与奶牛所在位置之间有段距离 求各个奶牛所要走的最大距离的最小值

思路: 先用 floyd求出各个奶牛到各个地方的最小距离   然后二分枚举  

根据枚举的值进行建图  求最大流  再根据求得最大流的值更换二分区间

一头奶牛只能去一个挤奶器挤奶 所以超级源点与奶牛之间的容量为1 

每个挤奶器最多可以给m头奶牛挤奶  所以超级汇点与挤奶器之间的容量为m

将奶牛与挤奶器之间的最短距离与枚举的值进行比较  如果小于枚举的值  说明这两个点之间的道路可以走的

根据这个思路建图  跑最大流 求最大值 更新区间


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 300
#define INF  100000000
#define ll __int64

using namespace std;

struct Dinic
{
    struct Edge
    {
        int from,to,cap,flow;
        Edge() {}
        Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
    };

    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];
    int d[MAXN];
    int cur[MAXN];
    int n,m,s,t,maxflow;

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<=n;i++)
            G[i].clear();
        edges.clear();
    }

    void addedge(int from,int to,int cap)
    {
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs()
    {
        MEM(vis,0);
        MEM(d,-1);
        queue<int> q;
        q.push(s);
        d[s]=maxflow=0;
        vis[s]=1;
        while(!q.empty())
        {
            int u=q.front(); q.pop();
            int sz=G[u].size();
            for(int i=0;i<sz;i++)
            {
                Edge e=edges[G[u][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    d[e.to]=d[u]+1;
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int u,int a)
    {
        if(u==t||a==0)  return a;
        int sz=G[u].size();
        int flow=0,f;
        for(int &i=cur[u];i<sz;i++)
        {
            Edge &e=edges[G[u][i]];
            if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[u][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)  break;
            }
        }
        return flow;
    }

    int Maxflow(int s,int t)
    {
        this->s=s; this->t=t;
        int flow=0;
        while(bfs())
        {
            MEM(cur,0);
            flow+=dfs(s,INF);
        }
        return flow;
    }
}Dic;

int dis[MAXN][MAXN];

int main()
{
    int k,c,m;
    while(scanf("%d%d%d",&k,&c,&m)!=EOF)
    {
        int n=k+c;
        int s=0; int t=n+1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&dis[i][j]);
                if(dis[i][j]==0)  dis[i][j]=INF;
            }
        }
        for(int k=1;k<=n;k++)
        {
            for(int i=1;i<=n;i++)
            {
                if(dis[i][k]!=INF)
                {
                    for(int j=1;j<=n;j++)
                    {
                        dis[i][j]=min(dis[i][k]+dis[k][j],dis[i][j]);
                    }
                }
            }
        }//floyd
        int l=0; int r=10000;
        while(l<r)
        {
            int mid=(l+r)/2;//mid为枚举的值  枚举两点之间的距离
            int ans=0;
            Dic.init(n+1);
            for(int i=k+1;i<=n;i++)  Dic.addedge(s,i,1);//超级源点与牛之间容量是1  每头牛只能去一个挤奶器
            for(int i=1;i<=k;i++)  Dic.addedge(i,t,m);//超级汇点与挤奶器之间容量是m 代表每个挤奶器最多给m头奶牛挤奶
            for(int i=k+1;i<=n;i++)
            {
                for(int j=1;j<=k;j++)
                {
                    if(dis[i][j]<=mid)  Dic.addedge(i,j,1);//奶牛与挤奶器之间的距离不大于枚举的值 说明这两个地方之间的道路可走
                }
            }//建边
            ans=Dic.Maxflow(s,t);
            if(ans>=c)  r=mid;//最大流大于等于牛的总数 表示当前枚举的值符合条件 且有可能会更小
            else  l=mid+1;
        }
        printf("%d\n",r);
    }
    return 0;
}




      





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值