POJ 2122 Optimal Milking

Optimal Milking

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 14040   Accepted: 5065
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头奶牛,k个挤奶机位置用编号1到k表示,奶牛位置用k+1到k+c编号来表示。输入是一个矩阵的形式,对角线表示与自身的位置距离为0,其他为0的位置表示没有边相互连接。问怎么安排每个奶牛到某个挤奶器挤奶,使得c头奶牛需要走的所有路程中最大路程最小。

分析:先用floyd算法求出任意两点之间的最短路,然后在用Dinic求出最大流;在搜索最大距离确定最小值时使用二分法。

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define INF 9999999
#define N 333
using namespace std;
int k,m,c;
int n;
int dis[N][N];//记录两点之间的最短路径
int mp[N][N];//容量网络
int qu[N*100];
int vis[N];//标记数组
int sign[N][N];//层次网络

void build_map(int x)
{
    memset(mp,0,sizeof mp);
    for(int i=k+1;i<=n;i++) mp[0][i]=1;
    for(int i=1;i<=k;i++) mp[i][n+1]=m;

    for(int i=k+1;i<=n;i++)
        for(int j=1;j<=k;j++)
        {
            if(dis[i][j]<=x) mp[i][j]=1;
        }
}

int bfs()//构建层次网络
{
    memset(vis,0,sizeof vis);
    memset(sign,0,sizeof sign);

    int qs=0,qe=1;
    vis[0]=1;
    qu[0]=0;

    while(qs<qe)
    {
        int v=qu[qs++];
        for(int i=0;i<=n+1;i++)
        {
            if(vis[i]==0 && mp[v][i])
            {
                vis[i]=1;
                qu[qe++]=i;
                sign[v][i]=1;
            }
        }
    }
    if(vis[n+1]) return 1;
    else return 0;
}

int dfs(int v,int sum)//dfs寻找增广路
{
    if(v==n+1) return sum;
    int s=sum;
    int t;

    for(int i=0;i<=n+1;i++)
    {
        if(sign[v][i])
        {
            t=dfs(i,min(sum,mp[v][i]));
            mp[v][i]-=t;
            mp[i][v]+=t;
            sum-=t;
        }
    }
    return s-sum;
}

int main()
{
    while(~scanf("%d%d%d",&k,&c,&m))
    {
        n=k+c;
        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 t=1;t<=n;t++)//floyd求解最短路
            for(int i=1;i<=n;i++)
            {
                if(dis[i][t]!=INF)
                    for(int j=1;j<=n;j++)
                    dis[i][j]=min(dis[i][j],dis[i][t]+dis[t][j]);
            }

        int le=0,ri=10009;
        int ans;
        while(le<ri)//二分找出最小的最大值
        {
            int mid=(le+ri)/2;
            ans=0;
            build_map(mid);//Dinic算法求最大流
            while(bfs()) ans+=dfs(0,INF);

            if(ans>=c) ri=mid;
            else le=mid+1;
        }

        printf("%d\n",ri);//满足条件的最小的mid用于更新ri
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 00:04:21

POJ 2122 Optimal Milking的相关文章

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

POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C≤200)头奶牛,在奶牛和挤奶器之间有一组不同长度的路.K个挤奶器的位置用1-K的编号标明,奶牛的位置用K+1-K+C 的编号标明.每台挤奶器每天最多能为M(1≤M≤15)头奶牛挤奶.寻找一个方案,安排每头奶牛到某个挤奶器挤奶,并使得C 头奶牛需要走的所有路程中的最大路程最小.每个测试数据中至少有一

POJ 2112 Optimal Milking(二分+最大流)

POJ 2112 Optimal Milking 题目链接 题意:给定一些机器和奶牛,在给定距离矩阵,(不在对角线上为0的值代表不可达),每个机器能容纳m个奶牛,问所有奶牛都能挤上奶,那么走的距离最大的奶牛的最小值是多少 思路:明显的二分+最大流,注意floyd求出的距离矩阵最大值可能不止200,所以二分的上限要注意 代码: #include <cstdio> #include <cstring> #include <queue> #include <algori

POJ 2112 Optimal Milking 最优挤奶方案 Floyd算法+二分查找+最大流

题目链接:POJ 2112 Optimal Milking Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12446   Accepted: 4494 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among

POJ 2112 Optimal Milking 二分答案+最大流

首先二分最长的边,然后删去所有比当前枚举的值长的边,算最大流,看是否能满足所有的牛都能找到挤奶的地方 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

POJ 2112—— Optimal Milking——————【多重匹配、二分枚举答案、floyd预处理】

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

POJ 2112 Optimal Milking

Optimal Milking Time Limit: 2000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 211264-bit integer IO format: %lld      Java class name: Main FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures amon

POJ 2112 Optimal Milking(最大流)

题目链接:http://poj.org/problem?id=2112 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

POJ 2112 Optimal Milking (二分 + 最大流)

题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 奶的情况下,走的最远的那头奶牛走的距离最小是多少. 数据保证有解. 算法讨论: 首先可以想到是二分,然后在选择流网络的时候,一开始选择的最小费用最大流,让二分的边权充当最小费用,但是这样跑发现每次二分的是我们要跑的答案,不可行.所以就改用最大流. 最大流肯定是在二分的情况下判定最大流是否等于c,即

poj 2112 Optimal Milking (二分图匹配的多重匹配)

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