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

来源:互联网 发布:淘宝买家秀震动棒 编辑:程序博客网 时间:2024/06/14 00:17

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 20 3 2 1 13 0 3 2 02 3 0 1 01 2 1 0 21 0 0 2 0

Sample Output

2

Source

USACO 2003 U S Open



题意:

k个机器,每个机器最多服务m头牛。

c头牛,每个牛需要1台机器来服务。

告诉你牛与机器每个之间的直接距离。

问:让所有的牛都被服务的情况下,使走的最远的牛的距离最短,求这个距离。



分析:

首先,一看到一个机器服务m个牛,一对多的关系,我们首先会想到用网络流。  (网络流)

其次,给定一个XXX条件,让你求满足这个条件的最小值/最大值,我们会直接想到 二分查找  (二分查找)

二分查找,可能是分的是最终答案,这个距离。那么远于这个距离的线段我们都要忽略掉,所以要重复建图  (重复建图)

重点是我们输入数据是i到j的距离,包括了牛到牛的距离,而我们要牛到挤奶器的最短距离,所以需要floyd算法求最短路  (最短路)


所以说,我们先保存数据。

对数据进行floyd,求出任意两点的最短路。

利用二分查找,对于每一个mid,从最短路中小于等于mid的值选择出来重新建图,建立一个原点和汇点,进行网络最大流,如果最大流的

最终结果等于C(c头牛),也就是mid距离下可以服务到所有的牛,那么我们把mid缩小。否则扩大。


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 310;
const int MAXM = 40010;
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow;
} edge[MAXM];
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
int k, c, m;
int s, e;
int map[MAXN][MAXN];
int mid;
int num;


void addedge(int u,int v,int w,int rw = 0)
{
    edge[tol].to = v;
    edge[tol].cap = w;
    edge[tol].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = rw;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}
int Q[MAXN];


void BFS(int start,int end)
{
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0] = 1;
    int front = 0, rear = 0;
    dep[end] = 0;
    Q[rear++] = end;
    while(front != rear)
    {
        int u = Q[front++];
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if(dep[v] != -1)continue;
            Q[rear++] = v;
            dep[v] = dep[u] + 1;
            gap[dep[v]]++;
        }
    }
}
int S[MAXN];
int sap(int start,int end,int N)
{
    BFS(start,end);
    memcpy(cur,head,sizeof(head));
    int top = 0;
    int u = start;
    int ans = 0;
    while(dep[start] < N)
    {
        if(u == end)
        {
            int Min = INF;
            int inser;
            for(int i = 0; i < top; i++)
                if(Min > edge[S[i]].cap - edge[S[i]].flow)
                {
                    Min = edge[S[i]].cap - edge[S[i]].flow;
                    inser = i;
                }
            for(int i = 0; i < top; i++)
            {
                edge[S[i]].flow += Min;
                edge[S[i]^1].flow -= Min;
            }
            ans += Min;
            top = inser;
            u = edge[S[top]^1].to;
            continue;
        }
        bool flag = false;
        int v;
        for(int i = cur[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].to;
            if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
            {
                flag = true;
                cur[u] = i;
                break;
            }
        }
        if(flag)
        {
            S[top++] = cur[u];
            u = v;
            continue;
        }
        int Min = N;
        for(int i = head[u]; i != -1; i = edge[i].next)
            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])return ans;
        dep[u] = Min + 1;
        gap[dep[u]]++;
        if(u != start)u = edge[S[--top]^1].to;
    }
    return ans;
}


void Foyld()
{
    for(int k = 1; k <= num; k++)
    {
        for(int i = 1; i <= num; i++)
        {
            for(int j = 1; j <= num; j++)
            {
                if(map[i][j] > map[i][k]+map[k][j])
                {
                    map[i][j] = map[i][k]+map[k][j];
                }
            }
        }
    }
}
void init()
{
    tol = 0;
    memset(head,-1,sizeof(head));
    for(int i = 1; i <= k; i++)
    {
        for(int j = k+1; j <= num; j++)
        {
            if(map[i][j] <= mid)
            {


                addedge(j,i,1);
            }
        }
    }
    for(int i = 1; i <= k; i++)
    {
        addedge(i,e,m);
    }
    for(int i = k+1; i <= num; i++)
    {
        addedge(s,i,1);
    }
}
int main()
{
    while(~scanf("%d%d%d",&k,&c,&m))
    {
        num = k+c;
        s = 0;
        e = num+1;
        int nv = num+2;
        for(int i = 1; i <= num; i++)
        {
            for(int j = 1; j <= num; j++)
            {
                scanf("%d",&map[i][j]);
                if(i!=j && !map[i][j])
                {
                    map[i][j] = INF;
                }
            }
        }
        Foyld();
        int l = 0, r = INF;
        while(l <= r)
        {
            mid = (r+l)/2;
            init();
            if(sap(s, e, nv) == c)
            {
                r = mid-1;
            }
            else
            {
                l = mid+1;
            }
        }
        printf("%d\n",l);
    }
    return 0;
}