POJ 2112 Optimal Milking 二分网络流

1人阅读 评论(0) 收藏 举报
分类:

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


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;


const int MAX_D=4000+5;
const int MAX_B=4000000+5;
const int INF=1e9;


int tot;
int mm[MAX_D][MAX_D];

int head[MAX_D];
int iter[MAX_D];
int level[MAX_D];
struct node{
    int to;
    int cap;
    int rev;
    int next;
};
node edge[2*MAX_B];




void add_edge(int a,int b,int c){


    edge[tot].to=b;
    edge[tot].cap=c;
    edge[tot].rev=tot+1;
    edge[tot].next=head[a];
    head[a]=tot++;


    edge[tot].to=a;
    edge[tot].cap=0;
    edge[tot].rev=tot-1;
    edge[tot].next=head[b];
    head[b]=tot++;


}




void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=head[v];i!=0;i=edge[i].next){
            node &e=edge[i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}


int dfs(int v,int t,int f){
    if(v==t)return f;
    for(int &i=iter[v];i!=0;i=edge[i].next){
        node &e = edge[i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                edge[e.rev].cap+=d;
                return d;
            }
        }
    }


    return 0;
}


int max_flow(int s,int t){
    int flow=0;
    while(1){
        bfs(s);
        if(level[t]<0)return flow;
        for(int i=0;i<MAX_D;i++){
            iter[i]=head[i];
        }
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}

void print(int x){
    cout<<endl;
    for(int i=1;i<=x;i++){
        for(int j=1;j<=x;j++){
            cout<<mm[i][j]<<" ";
        }
        cout<<endl;
    }
}


void solve(){
    int k,c,m;
    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",&mm[i][j]);
                if(mm[i][j]==0&&i!=j){
                    mm[i][j]=INF;
                }
            }
        }
        // print(k+c);

        for(int i=1;i<=k+c;i++){
            for(int j=1;j<=k+c;j++){
                for(int w=1;w<=k+c;w++){
                    if(mm[j][w]>mm[j][i]+mm[i][w]){
                        mm[j][w]=mm[j][i]+mm[i][w];
                    }
                }
            }
        }
        //print(k+c);

        int left=0,right=INF;

        while(left<=right){
            int mid=(left+right)>>1;
            tot=1;
            memset(head,0,sizeof(head));
            for(int i=1;i<=k;i++){
                add_edge(1,i+3,m);
            }
            for(int i=1;i<=c;i++){
                add_edge(i+k+3,2,1);
            }

            for(int i=1;i<=k;i++){
                for(int j=1;j<=c;j++){
                    if(mm[i][j+k]<=mid){
                        add_edge(i+3,j+k+3,1);
                    }
                }
            }
            int xxx=max_flow(1,2);
            if(xxx==c){
                right=mid-1;
            }
            else{
                left=mid+1;
            }


        }

        printf("%d\n",left);

    }





}


int main()
{
    solve();


    return 0;
}

查看评论

POJ-2112 Optimal Milking

题目大意: 有k个挤奶器,在牧场里有c头奶牛,每个挤奶器可以满足m个奶牛,奶牛和挤奶器都可以看成是实体,现在给出两个实体之间的距离,如果没有路径相连,则为0,现在问你在所有方案里面,这c头奶牛需要走...
  • Snow_Me
  • Snow_Me
  • 2016-08-08 22:15:45
  • 304

POJ 2112 Optimal Milking(最大流)

POJ 2112 Optimal Milking(最大流)
  • u012860063
  • u012860063
  • 2015-07-19 10:16:33
  • 1202

POJ 2112 Optimal Milking 已翻译

描述 FJ已经将他的K(1 不同长度的一组路径在牛和挤奶机中运行。挤奶机位置由ID号1..K;母牛位置由ID号K + 1..K + C命名。 ...
  • woniupengpeng
  • woniupengpeng
  • 2016-12-17 18:44:29
  • 150

POJ 2112 Optimal Milking (网络流+二分)

首先floyd求出任意两点间最短路。 然后二分路径长度的最大值。 每次二分中: 1对于所有长度小于等于这个最大值的 从奶牛到机器的路径,建一条流量为无限的边。 2.再从源点到每头牛建一条流量为1...
  • Baoli1008
  • Baoli1008
  • 2015-07-23 11:49:09
  • 210

Optimal Milking (poj 2112 网络流+二分+floyd)

题意:农场有K个挤奶器和C头奶牛,每个挤奶器和奶牛都在不同的位置,挤奶器编号1~K,奶牛编号K+1~K+C,邻接矩阵给出它们之间的距离,每台挤奶器每天最多能为M头牛挤奶。寻找一个方案,安排每头奶牛到某...
  • u014422052
  • u014422052
  • 2015-02-19 19:40:05
  • 441

POJ 2112 Optimal Milking【网络流+二分+最短路】

求使所有牛都可以被挤牛奶的条件下牛走的最长距离。 Floyd求出两两节点之间的最短路,然后二分距离。 构图: 将每一个milking machine与源点连接,边权为最大值m,每个cow与汇点连...
  • yang_7_46
  • yang_7_46
  • 2013-06-17 14:20:21
  • 888

POJ 2112 Optimal Milking (floyd + 二分 + 网络流)

FLOYD预处理出每两点之间的最短距离,二分查找判断
  • u013967323
  • u013967323
  • 2014-08-04 15:48:06
  • 497

【POJ 2112 Optimal Milking】网络流 & 二分 & floyd

Optimal Milking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 18478 Accep...
  • WYK1823376647
  • WYK1823376647
  • 2017-10-02 16:35:54
  • 89

POJ 2112 Optimal Milking (网络流+二分)

Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 9267  ...
  • BehappyXiang
  • BehappyXiang
  • 2013-03-06 11:12:21
  • 465

poj 2112 Optimal Milking 二分最短路网络流

Description FJ has moved his K (1
  • Little_boy_z
  • Little_boy_z
  • 2017-09-28 19:31:04
  • 56
    个人资料
    持之以恒
    等级:
    访问量: 1428
    积分: 692
    排名: 7万+
    文章存档
    最新评论