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

Source

题意:有k台挤奶器,每台挤奶器最多容纳m头奶牛,该牧场共同拥有c头奶牛,如今给定这k台机器和c头奶牛相互间的直接距离,求让全部奶牛到达挤奶器且满足该条件时奶牛走到挤奶器间的最大距离的最小值。

题解:构图:先用Floyd求出相互间的最短距离,然后设置源点到每头牛的距离为1,每台机器到汇点的距离为m,然后若牛到机器的距离不大于maxdist,那么则将该边增加到新图中,最后对新图求最大流,推断最大流是否等于c,就这样二分枚举maxdist直到找到最小的maxdist为止。

#include <stdio.h>
#include <string.h>
#define inf 0x3fffffff
#define maxn 235 int dist[maxn][maxn], k, c, m, n;
int G[maxn][maxn], Layer[maxn];
int queue[maxn], maxDist;
bool vis[maxn]; void Floyd() {
int x, i, j;
maxDist = 200;
for(x = 1; x <= n; ++x)
for(i = 1; i <= n; ++i)
for(j = 1; j <= n; ++j)
if(dist[i][j] > dist[i][x] + dist[x][j]) {
dist[i][j] = dist[i][x] + dist[x][j];
if(maxDist < dist[i][j]) maxDist = dist[i][j];
}
} void build(int flow) {
memset(G, 0, sizeof(G));
int i, j;
for(i = k + 1; i <= n; ++i) {
G[0][i] = 1;
for(j = 1; j <= k; ++j)
if(dist[i][j] <= flow)
G[i][j] = 1;
}
for(j = 1; j <= k; ++j)
G[j][n + 1] = m;
} bool countLayer() {
int id = 0, front = 0, now, i;
memset(Layer, 0, sizeof(Layer));
Layer[0] = 1; queue[id++] = 0;
while(front < id) {
now = queue[front++];
for(i = 0; i <= n + 1; ++i)
if(G[now][i] && !Layer[i]) {
Layer[i] = Layer[now] + 1;
if(i == n + 1) return true;
else queue[id++] = i;
}
}
return false;
} bool Dinic() {
int i, maxFlow = 0, id = 0, now, minCut, pos, u, v;
while(countLayer()) {
memset(vis, 0, sizeof(vis));
vis[0] = 1; queue[id++] = 0;
while(id) {
now = queue[id - 1];
if(now == n + 1) {
minCut = inf;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
if(G[u][v] < minCut) {
minCut = G[u][v];
pos = u;
}
}
maxFlow += minCut;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
G[u][v] -= minCut;
G[v][u] += minCut;
}
while(id && queue[id - 1] != pos)
vis[queue[--id]] = 0;
} else {
for(i = 0; i <= n + 1; ++i) {
if(G[now][i] && !vis[i] && Layer[now] + 1 == Layer[i]) {
queue[id++] = i;
vis[i] = 1; break;
}
}
if(i > n + 1) --id;
}
}
}
return maxFlow == c;
} int binarySolve() {
int left = 0, right = maxDist, mid;
while(left < right) {
mid = (left + right) >> 1;
build(mid);
if(Dinic()) right = mid;
else left = mid + 1;
}
return left;
} int main() {
//freopen("stdin.txt", "r", stdin);
int i, j;
while(scanf("%d%d%d", &k, &c, &m) == 3) {
for(i = 1, n = k + c; i <= n; ++i)
for(j = 1; j <= n; ++j) {
scanf("%d", &dist[i][j]);
if(!dist[i][j] && i != j)
dist[i][j] = inf;
}
Floyd();
printf("%d\n", binarySolve());
}
return 0;
}

POJ2112 Optimal Milking 【最大流+二分】的更多相关文章

  1. [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)

    http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...

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

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

  3. POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  4. POJ2112 Optimal Milking

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 17811   Accepted: 6368 ...

  5. POJ2112 Optimal Milking (网络流)(Dinic)

                                             Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K T ...

  6. POJ-2112 Optimal Milking(floyd+最大流+二分)

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

  7. POJ2112 Optimal Milking(最大流)

    先Floyd求牛到机器最短距离,然后二分枚举最长的边. #include<cstdio> #include<cstring> #include<queue> #in ...

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

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

  9. poj2112 Optimal Milking --- 最大流量,二分法

    nx一个挤奶器,ny奶牛,每个挤奶罐为最m奶牛使用. 现在给nx+ny在矩阵之间的距离.要求使所有奶牛挤奶到挤奶正在旅程,最小的个体奶牛步行距离的最大值. 始感觉这个类似二分图匹配,不同之处在于挤奶器 ...

随机推荐

  1. Linux中删除文件,磁盘空间未释放问题追踪

    在客户使用我们产品后,发现一个问题:在删除了文件后.磁盘空间却没有释放.是有进程在打开这个文件,还是其它情况?我们一起来看看一下两个场景 一. 场景一:进程打开此文件 当一个文件正在被一个进程使用时. ...

  2. SICP 习题 (2.10)解题总结: 区间除法中除于零的问题

    SICP 习题 2.10 要求我们处理区间除法运算中除于零的问题. 题中讲到一个专业程序猿Ben Bitdiddle看了Alyssa的工作后提出了除于零的问题,大家留意一下这个叫Ben的人,后面会不断 ...

  3. PHP版本 D-Link 动态域名客户端

    <?php /* * D-Link 动态域名客户端.主域名www.dlinkddns.com 和 www.dlinkddns.com.cn * 首先获取外网IP,若IP没有变化,则结束运行:否则 ...

  4. String字符串操作--切割,截取,替换,查找,比较,去空格.....

    字符串拼接 直接用+号:String a = "I"; String b = "love"; String c = "you";String ...

  5. HTML基础第四讲---图像

    转自:https://blog.csdn.net/likaier/article/details/326735 图像,也就是images,在html语法中用img来表示,其基本的语法是:   < ...

  6. 94.文件bat脚本自删除

    taskkill / f / im 自删除.exedel 自删除.exedel 1.bat void main() { FILE *pf = fopen("1.bat", &quo ...

  7. Android RecyclerView嵌套RecyclerView

    原理 RecyclerView嵌套RecyclerView的条目,项目中可能会经常有这样的需求,但是我们将子条目设置为RecyclerView之后,却显示不出来.自己试了很久,终于找到了原因:必须先设 ...

  8. zeromq and jzmq

    install c test install jzmq java test Storm UI Cluster Summary Version Nimbus uptime Supervisors Use ...

  9. debian 9 安装后需做的几件事

    debian 9 安装后需做的几件事 安装环境:X86 >> Debian 9 Linux/GNU apt源更新 注意连上有线网络 刚安装好的debian系统中,/etc/apt/sour ...

  10. Cocos2d-x学习笔记(一)HelloWorld

    原创文章,转载请注明出处:http://blog.csdn.net/sfh366958228/article/details/38656755 前言 正式来公司实习已有一月,前一月主要是看了<C ...