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 头奶牛, 每台机器最多可供喂养的奶牛的数量 m , 开始给出任意两点之间的距离,你可以随意的安置奶牛,要求奶牛走的最远的距离最小
思路分析 :
  首先可以用 floyd 跑出任意两点的最短路
  然后就是一个网络流即可,用源点和每个机器建立一条边,流量为 m , 用汇点和每头牛建立一条边,流量为 1,并且二分答案,当机器和牛的距离小于 mid 时,即可连边,每次更新答案即可
代码示例 :
const int maxn = 1e4+5;
const int inf = 0x3f3f3f3f; int k, c, m;
struct node
{
int to, next;
int flow;
}e[maxn<<1];
int head[maxn];
int cnt;
int mp[300][300]; void addedge(int u, int v, int w){
e[cnt].to = v, e[cnt].flow = w, e[cnt].next = head[u], head[u] = cnt++;
e[cnt].to = u, e[cnt].flow = 0, e[cnt].next = head[v], head[v] = cnt++;
} void flod() {
int sum = k+c;
for(int f = 1; f <= sum; f++){
for(int i = 1; i <= sum; i++){
for(int j = 1; j <= sum; j++){
mp[i][j] = min(mp[i][f]+mp[f][j], mp[i][j]);
}
}
}
} int dep[maxn], que[maxn];
bool bfs(int s, int t){
memset(dep, 0, sizeof(dep));
int head1 = 0, tail = 1; dep[s] = 1;
que[0] = s;
while(head1 < tail) {
int u = que[head1++];
for(int i = head[u]; i != -1; i = e[i].next) {
int to = e[i].to;
if (e[i].flow && !dep[to]) {
dep[to] = dep[u]+1;
que[tail++] = to;
}
}
}
return dep[t];
}
int aim;
int dfs(int u, int f1){
if (u == aim || f1 == 0) return f1; int f = 0;
for(int i = head[u]; i != -1; i = e[i].next){
int to = e[i].to;
if (e[i].flow && dep[to] == dep[u]+1){
int x = dfs(to, min(f1, e[i].flow));
e[i].flow -= x; e[i^1].flow += x;
f1 -= x, f += x;
if (f1 == 0) return f;
}
}
if (!f) dep[u] = -2;
return f;
} bool check(int lenth){
int s = 0, t = k+c+1;
memset(head, -1, sizeof(head));
cnt = 0; aim = t; for(int i = 1; i <= k; i++) addedge(s, i, m);
for(int i = k+1; i <= k+c; i++) addedge(i, t, 1);
for(int i = 1; i <= k; i++){
for(int j = k+1; j <= k+c; j++){
if (mp[i][j] <= lenth) addedge(i, j, 1);
}
}
int res = 0;
while(bfs(s, t)){
res += dfs(s, inf);
}
if (res == c) return true;
return false;
} int main() {
int x; cin >> k >> c >> m;
memset(mp, inf, sizeof(mp));
for(int i = 1; i <= k+c; i++){
for(int j = 1; j <= k+c; j++){
scanf("%d", &x);
if (x != 0) mp[i][j] = x;
}
}
flod();
int l = 0, r = 1e5;
int ans;
while(l <= r){
int mid = (l+r)>>1;
if (check(mid)) {ans = mid; r = mid-1;}
else l = mid+1;
}
cout <<ans << endl;
return 0;
}

floyd + 最大流 (奶牛分配问题)的更多相关文章

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

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

  2. [ZOJ2760]How Many Shortest Path(floyd+最大流)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...

  3. BZOJ 2324: [ZJOI2011]营救皮卡丘( floyd + 费用流 )

    昨晚写的题...补发一下题解... 把1~N每个点拆成xi, yi 2个. 预处理i->j经过编号不超过max(i,j)的最短路(floyd) S->0(K, 0), S->xi(1 ...

  4. POJ 2391 Ombrophobic Bovines (二分答案+floyd+最大流)

    <题目链接> 题目大意: 给定一个有$n$个顶点和$m$条边的无向图,点$i$ 处有$A_i$头牛,点$i$ 处的牛棚能容纳$B_i$头牛,每条边有一个时间花费$t_i$(表示从一个端点走 ...

  5. poj 2391 (Floyd+最大流+二分)

    题意:有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 两个避雨点间可以相互到 ...

  6. POJ1336 The K-League[最大流 公平分配问题]

    The K-League Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 715   Accepted: 251 Descri ...

  7. bzoj 2324 [ZJOI2011]营救皮卡丘(floyd,费用流)

    2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1777  Solved: 712[Submit][Stat ...

  8. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  9. 【ACM】那些年,我们挖(WA)过的最短路

    不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...

随机推荐

  1. CSS滤镜 :灰色 ,方便站点哀悼

    html {  -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); ...

  2. httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete

    httpclient post方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 //----1. HttpPost request = new HttpPost(ur ...

  3. POJ 3660 Cow Contest(floyed运用)

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

  4. spring的几个面试题

    Spring 是一种轻量级开发框架,旨在提高开发人员的开发效率以及系统的可维护性.Spring 官网:https://spring.io/. 我们一般说 Spring 框架指的都是 Spring Fr ...

  5. redisUtil

    package com.cinc.ecmp.utils; import org.springframework.beans.factory.annotation.Autowired; import o ...

  6. 基于bmob后端云小程序开发——口袋吉他

    人的一生90%的时间都在做着无聊的事情,社会的发展使得我们的闲暇时间越来越多,我们把除了工作的其他时间放在各种娱乐活动上. 程序员有点特殊,他们把敲代码看成娱乐活动的一部分,以此打发时间的不占少数.这 ...

  7. UTF-8、UTF-16、UTF-32编码的相互转换(不使用现成的函数)

    最近在考虑写一个可以跨平台的通用字符串类,首先需要搞定的就是编码转换问题. vs默认保存代码文件,使用的是本地code(中文即GBK,日文即Shift-JIS),也可以使用带BOM的UTF-8.gcc ...

  8. elasticsearch基础知识杂记

    日常工作中用到的ES相关基础知识和总结.不足之处请指正,会持续更新. 1.集群的健康状况为 yellow 则表示全部主分片都正常运行(集群可以正常服务所有请求),但是 副本 分片没有全部处在正常状态. ...

  9. 关于python2和python3除法的区别

    在Python2中,除法的取值结果取整数 >>> 9/2 4 而在Python3中,除法/的结果包含小数,如果只想取整数需要使用// >>> 9/2 4.5 > ...

  10. 【python测试开发栈】—帮你总结Python os模块高频使用的方法

    Python中的os模块是主要和系统操作相关的模块,在平时的工作中会经常用到,花时间整理了os模块的高频使用方法,同时整理出使用时需要注意的点.归纳来讲,os模块的方法可以分为:目录操作.文件操作.路 ...