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

  1. 2 3 2
  2. 0 3 2 1 1
  3. 3 0 3 2 0
  4. 2 3 0 1 0
  5. 1 2 1 0 2
  6. 1 0 0 2 0

Sample Output

  1. 2

  2. 题意:有K台挤奶机,C头奶牛,每台挤奶机可以容纳M头奶牛,挤奶机和奶牛两两之间都有个距离,现在问在保证所有的奶牛都可以产奶的情况下,走到挤奶机需要走最远的奶牛的最短要走的距离是多少?
    题解:先用floyed算法算出每头奶牛和挤奶机之间的最短路径,在保证所有奶牛都能够产奶的情况下二分求解,设立超级源点SS向每台挤奶机之间连容量为M的边,每台挤奶机向奶牛连容量为1的边,所有奶牛
    向超级汇点连容量为1的边,求解最大流。
  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <queue>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <iostream>
  7. #include <math.h>
  8. using namespace std;
  9. const int N = ;
  10. const int INF = ;
  11. struct Edge
  12. {
  13. int v,next;
  14. int w;
  15. } edge[N*N];
  16. int head[N];
  17. int level[N];
  18. int tot;
  19. void init()
  20. {
  21. memset(head,-,sizeof(head));
  22. tot=;
  23. }
  24. void addEdge(int u,int v,int w,int &k)
  25. {
  26. edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
  27. edge[k].v = u,edge[k].w=,edge[k].next=head[v],head[v]=k++;
  28. }
  29. int BFS(int src,int des)
  30. {
  31. queue<int >q;
  32. memset(level,,sizeof(level));
  33. level[src]=;
  34. q.push(src);
  35. while(!q.empty())
  36. {
  37. int u = q.front();
  38. q.pop();
  39. if(u==des) return ;
  40. for(int k = head[u]; k!=-; k=edge[k].next)
  41. {
  42. int v = edge[k].v;
  43. int w = edge[k].w;
  44. if(level[v]==&&w!=)
  45. {
  46. level[v]=level[u]+;
  47. q.push(v);
  48. }
  49. }
  50. }
  51. return -;
  52. }
  53. int dfs(int u,int des,int increaseRoad)
  54. {
  55. if(u==des||increaseRoad==) return increaseRoad;
  56. int ret=;
  57. for(int k=head[u]; k!=-; k=edge[k].next)
  58. {
  59. int v = edge[k].v,w=edge[k].w;
  60. if(level[v]==level[u]+&&w!=)
  61. {
  62. int MIN = min(increaseRoad-ret,w);
  63. w = dfs(v,des,MIN);
  64. if(w > )
  65. {
  66. edge[k].w -=w;
  67. edge[k^].w+=w;
  68. ret+=w;
  69. if(ret==increaseRoad) return ret;
  70. }
  71. else level[v] = -;
  72. if(increaseRoad==) break;
  73. }
  74. }
  75. if(ret==) level[u]=-;
  76. return ret;
  77. }
  78. int Dinic(int src,int des)
  79. {
  80. int ans = ;
  81. while(BFS(src,des)!=-) ans+=dfs(src,des,INF);
  82. return ans;
  83. }
  84. int graph[N][N];
  85. int k,c,m;
  86. int floyed(int n)
  87. {
  88. int MAX=-;
  89. for(int k=; k<=n; k++)
  90. {
  91. for(int i=; i<=n; i++)
  92. {
  93. for(int j=; j<=n; j++)
  94. {
  95. graph[i][j] = min(graph[i][j],graph[i][k]+graph[k][j]);
  96. }
  97. }
  98. }
  99. for(int i=;i<=n;i++){
  100. for(int j=;j<=n;j++){
  101. if(graph[i][j]!=INF)
  102. MAX = max(MAX,graph[i][j]);
  103. }
  104. }
  105. return MAX;
  106. }
  107. int build(int v){
  108. init();
  109. int src = ,des = k+c+;
  110. for(int i=;i<=k;i++) addEdge(src,i,m,tot);
  111. for(int i=k+;i<=k+c;i++) addEdge(i,des,,tot);
  112. for(int i=;i<=k;i++){
  113. for(int j=k+;j<=k+c;j++){
  114. if(graph[i][j]<=v) addEdge(i,j,,tot);
  115. }
  116. }
  117. return Dinic(src,des);
  118. }
  119. int main()
  120. {
  121. while(scanf("%d%d%d",&k,&c,&m)!=EOF)
  122. {
  123. for(int i=;i<=k+c;i++){
  124. for(int j=;j<=k+c;j++){
  125. scanf("%d",&graph[i][j]);
  126. if(graph[i][j]==&&i!=j) graph[i][j] = INF;
  127. }
  128. }
  129. int MAX = floyed(k+c);
  130. int l=,r = MAX;
  131. int ans = MAX;
  132. while(l<=r){
  133. int mid = (l+r)>>;
  134. if(build(mid)==c) {
  135. ans = mid;
  136. r = mid-;
  137. }
  138. else l =mid+;
  139. }
  140. printf("%d\n",ans);
  141. }
  142. }

poj 2112(二分+网络流)的更多相关文章

  1. poj 2112(二分+多重匹配)

    题目链接:http://poj.org/problem?id=2112 思路:由于要求奶牛走的最远距离的最短路程,显然我们可以二分距离,如果奶牛与挤奶器的距离小于等于limit的情况下,能够满足,则在 ...

  2. POJ 2112 二分+最大流

    Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 17297   Accepted: 6203 ...

  3. POJ 2455 二分+网络流

    题意: 思路: 莫名其妙TLE 啊woc我A了一坨题的网络流模板有问题 !!!! 在常数上会慢 (一个等于号 啊啊啊) 改了所有网络流有关的文章- .... //By SiriusRen #inclu ...

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

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

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

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

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

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

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

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

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

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

  9. POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

    Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: ...

随机推荐

  1. jenkins 全局工具配置

  2. Jenkins注意点

    这里要填写 在 Linux 上 生成的 git 私钥 并且带上  前后 注释 ------start -----      ---end -------

  3. Thinkphp5的安装

    很长没有码代码了,现在开始做这件事情的意义已经完全与以前不一样了.因为最近有相当长的一段休息时间,是个学习的好时间啊.之前接触过TP3.2,听说后来的版本有挺大的改动,因此呢,现在终于有时间可以好好的 ...

  4. mysql主主复制汇总整理

    mysql主主复制汇总整理 一.Mysql主主.主从复制主要思路: 1.mysql复制实质: 就是其他的MySQL数据库服务器将这个数据变更的二进制日志在本机上再执行一遍,因此非常重要的一点是mysq ...

  5. leetcode-15-basic-string

    58. Length of Last Word 解题思路: 从结尾向前搜索,空格之前的就是最后一个词了.写的时候我考虑了尾部有空格的情况.需要注意的是,测试用例中有" "的情况,此 ...

  6. How to setup multimedia on CentOS 7

    You will need to also install the EPEL repository as nux-dextop depends on this for some of its pack ...

  7. CodeForce:732B-Cormen — The Best Friend Of a Man

    传送门:http://codeforces.com/problemset/problem/732/B Cormen - The Best Friend Of a Man time limit per ...

  8. debian软raid

    http://www.linuxidc.com/Linux/2013-06/86487.htm  

  9. bash之条件测试if/else

    bash之条件测试:     if/then结构         条件测试(CONDITION):         test EXPRESSION:测试条件表达式正确否         [ EXPRE ...

  10. P3369 【模板】普通平衡树 Treap

    P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...