题目链接: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 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

Source

题目描写叙述:(转)

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

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

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

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

解题报告:

二分枚举距离,实际距离满足当前枚举距离限制的能够增加这条边。

枚举的距离中符合条件的最小值就是答案。

建图过程:

一个超级原点,和每一个机器的容量都是m。

一个超级汇点,每头牛和汇点的容量都是1.

机器i与牛j之间的距离假设小于等于当前枚举值mid,连接i,j。容量1.

这样最大流的意义就是可以服务的牛最多是多少,假设最大流等于牛的总数c。表示当前枚举值mid符合条件,同一时候说明mid值还可能可以更小。更新二分右边界r = mid - 1.

假设小于牛的总数。说明mid偏小,更新二分左边界,l = mid + 1.

机器与牛之间的最短距离能够用floyd预处理出来。

代码例如以下:

#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]; //注意是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++)//k个挤奶器
{
for(int j = k+1; j <= num; j++)//c头奶牛
{
if(map[i][j] <= mid)
{
//假设奶牛到挤奶器的最短距离<=mid,建权值为1的边
addedge(j,i,1);
}
}
}
for(int i = 1; i <= k; i++)
{
addedge(i,e,m);//每一个挤奶器最多能够挤k头牛
}
for(int i = k+1; i <= num; i++)
{
addedge(s,i,1);//建一条源点到奶牛的边。权值为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)//最大流等于c
{
r = mid-1;
}
else
{
l = mid+1;
}
}
printf("%d\n",l);
}
return 0;
}

POJ 2112 Optimal Milking(最大流)的更多相关文章

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

    题目链接 测试dinic模版,不知道这个模版到底对不对,那个题用这份dinic就是过不了.加上优化就WA,不加优化TLE. #include <cstdio> #include <s ...

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

    POJ 2112 Optimal Milking 链接:http://poj.org/problem?id=2112 题意:农场主John 将他的K(1≤K≤30)个挤奶器运到牧场,在那里有C(1≤C ...

  3. POJ 2112 Optimal Milking (二分+最短路径+网络流)

    POJ  2112 Optimal Milking (二分+最短路径+网络流) Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K To ...

  4. Poj 2112 Optimal Milking (多重匹配+传递闭包+二分)

    题目链接: Poj 2112 Optimal Milking 题目描述: 有k个挤奶机,c头牛,每台挤奶机每天最多可以给m头奶牛挤奶.挤奶机编号从1到k,奶牛编号从k+1到k+c,给出(k+c)*(k ...

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

    题目大意: 在一个农场里面,有k个挤奶机,编号分别是 1..k,有c头奶牛,编号分别是k+1 .. k+c,每个挤奶机一天最让可以挤m头奶牛的奶,奶牛和挤奶机之间用邻接矩阵给出距离.求让所有奶牛都挤到 ...

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

    Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Sub ...

  7. POJ 2112 Optimal Milking (Dinic + Floyd + 二分)

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 19456   Accepted: 6947 ...

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

    http://poj.org/problem?id=2112 题意: 现在有K台挤奶器和C头奶牛,奶牛和挤奶器之间有距离,每台挤奶器每天最多为M头奶挤奶,现在要安排路程,使得C头奶牛所走的路程中的最大 ...

  9. POJ - 2112 Optimal Milking (dijkstra + 二分 + 最大流Dinic)

    (点击此处查看原题) 题目分析 题意:在一个农场中有k台挤奶器和c只奶牛,每个挤奶器最多只能为m只奶牛挤奶,每个挤奶器和奶牛都视为一个点,将编号1~k记为挤奶器的位置,编号k+1~k+c记为奶牛的位置 ...

随机推荐

  1. bzoj1375 双调路径

    Description 来越多,因此选择最佳路径是很现实的问题.城市的道路是双向的,每条道路有固定的旅行时间以及需要支付的费用.路径由连续的道路组成.总时间是各条道路旅行时间的和,总费用是各条道路所支 ...

  2. Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈

    B. Mike and Feet Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...

  3. 读书笔记_Effective_C++_条款二十七:尽量少做转型动作

    有关转型的几种做法,已经在早些的博客中写过了.这里先简单回顾一下,再讲一讲effective中对之更深入的阐述. 转型可以按风格可以分成C风格转型和C++风格转型两大类,C风格转型很容易看到,因为我们 ...

  4. Windows用WinDbg分析蓝屏dump文件查找原因(转)

    WinDbg官方下载: http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.11.1.404.msi http://msdl. ...

  5. LT1946A-- Transformerless dc/dc converter produces bipolar outputs

    Dual-polarity supply provides ±12V from one IC VC (Pin 1): Error Amplifier Output Pin. Tie external ...

  6. XuLA/XuLA2

    http://www.xess.com/prods/prod048.php XuLA http://www.xess.com/prods/prod055.php XuLA2 http://www.xe ...

  7. Unity3D开发之查找面板上某个脚本(包含Missing)

    有时候我们须要知道某个脚本在场景上面哪里用到,或者那个脚本被删除了但又没有把相关游戏场景的关联东西删掉,那样我们就要一个脚本来查找一下了: PS:以下两个脚本都要放到assets/Editor以下哦. ...

  8. 【GISER&&Painter】Chapter01:WebGL渲染初体验

    基于上一篇OpenGL的渲染原理,这两周又陆续接触了一些关于WebGL绘图的一些内容,因为刚入门,很多东西又很晦涩,所以特意花了小半天的时间整理了一下,特此记录. 零  画一个多边形吧! 把一个多边形 ...

  9. iOS中 imageNamed方法 非常多图片占用大量内存问题

    当我们须要载入非常多图片(相冊)的时候我们通常会用[UIimage  imageNamed:imageName]; 实际上[UIimage  imageNamed:imageName]这种方法在图片使 ...

  10. android组件之DrawerLayout(抽屉导航)-- 侧滑菜单效果

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/41696291 一. 介绍     导航抽屉显示在屏幕的最左侧,默认情况下是隐藏的,当用 ...