Optimal Milking
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 19347   Accepted: 6907
Case Time Limit: 1000MS
issions: 19347   Accepted: 6907
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

Source

求行走距离的最远的奶牛的至少要走多远。

注意要先用Floyd求每两点之间的最短路。。。。。。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <queue>
  7. #define mem(a,b) memset(a,b,sizeof(a))
  8. using namespace std;
  9. const int maxn = , INF = 0x3f3f3f3f;
  10. typedef long long LL;
  11.  
  12. int head[maxn], d[maxn], vis[maxn], p[maxn], f[maxn], way[][];
  13. int n, m, s, t, neng;
  14. int cnt, flow, value;
  15.  
  16. struct node{
  17. int u, v, c, w, next;
  18. }Node[];
  19.  
  20. void add_(int u, int v, int c, int w)
  21. {
  22. Node[cnt].u = u;
  23. Node[cnt].v = v;
  24. Node[cnt].c = c;
  25. Node[cnt].w = w;
  26. Node[cnt].next = head[u];
  27. head[u] = cnt++;
  28. }
  29.  
  30. void add(int u, int v, int c, int w)
  31. {
  32. add_(u, v, c, w);
  33. add_(v, u, , -w);
  34. }
  35.  
  36. int spfa()
  37. {
  38. queue<int> Q;
  39. for(int i=; i<maxn; i++) d[i] = INF;
  40. d[s] = ;
  41. mem(vis, );
  42. mem(p, -);
  43. Q.push(s);
  44. vis[s] = ;
  45. p[s] = ; f[s] = INF;
  46. while(!Q.empty())
  47. {
  48. int u = Q.front(); Q.pop();
  49. vis[u] = ;
  50. for(int i=head[u]; i!=-; i=Node[i].next)
  51. {
  52. node e = Node[i];
  53. if(d[e.v] > max(d[e.u], e.w) && e.c > )
  54. {
  55. d[e.v] = max(d[e.u], e.w);
  56. p[e.v] = i;
  57. f[e.v] = min(f[u], e.c);
  58. if(!vis[e.v])
  59. {
  60. Q.push(e.v);
  61. vis[e.v] = ;
  62. }
  63. }
  64. }
  65. }
  66. if(p[t] == -) return ;
  67. // cout<< value <<endl;
  68. flow += f[t], value = d[t];
  69. for(int i=t; i!=s; i=Node[p[i]].u)
  70. {
  71. Node[p[i]].c -= f[t];
  72. Node[p[i]^].c += f[t];
  73. }
  74. return ;
  75. }
  76.  
  77. void max_flow()
  78. {
  79. while(spfa());
  80. printf("%d\n",value);
  81. }
  82.  
  83. int main()
  84. {
  85. mem(head, -);
  86. mem(way, INF);
  87. cnt = ;
  88. scanf("%d%d%d", &n, &m, &neng);
  89. for(int i=; i<=n+m; i++)
  90. way[i][i] = ;
  91.  
  92. for(int i=; i<=n+m; i++)
  93. {
  94. for(int j=; j<=n+m; j++)
  95. {
  96. int w;
  97. scanf("%d",&w);
  98. if(w) way[i][j] = w;
  99.  
  100. }
  101. }
  102. for(int k=;k<=n+m;k++)
  103. for(int i=;i<=n+m;i++)
  104. for(int j=;j<=n+m;j++)
  105. way[i][j]=min(way[i][j],way[i][k]+way[k][j]);
  106. for(int i=; i<=m; i++)
  107. for(int j=; j<=n; j++)
  108. if(way[n+i][j] < INF)
  109. add(n+i, j, , way[n+i][j]);
  110.  
  111. s = ; t = n + m + ;
  112. for(int i=; i<=m; i++)
  113. add(s, n+i, , );
  114. for(int j=; j<=n; j++)
  115. add(j, t, neng, );
  116. max_flow();
  117.  
  118. return ;
  119. }

Optimal Milking POJ - 2112 (多重最优匹配+最小费用最大流+最大值最小化 + Floyd)的更多相关文章

  1. N - Optimal Milking - POJ 2112(二分图多重匹配+Floyd+二分搜索)

    题意:有K太挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 分析:应该先使用floyd求出来点之间的最短路??(不晓得给 ...

  2. POJ 2195:Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...

  3. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

  4. POJ 2135.Farm Tour 消负圈法最小费用最大流

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4914   Accepted: 1284   ...

  5. POJ 2195 Going Home(最小费用最大流)

    http://poj.org/problem?id=2195 题意 :  N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...

  6. POJ 2135 Farm Tour (最小费用最大流模板)

    题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...

  7. POJ 3422 Kaka&#39;s Matrix Travels (最小费用最大流)

    POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...

  8. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  9. poj 3422(最小费用最大流)

    题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...

随机推荐

  1. jqgrid 选中行触发编辑,切换下一行时验证和异步保存上一行数据

    有时,我们需要批量修改或填写一些相似的数据.可以以jqgrid表来显示,可能的效果如下: 选中触发行编辑参考:jqgrid 单击行启用行编辑,切换行保存原编辑行 本文主要说说验证和异步保存上一条数据的 ...

  2. 开关电源PCB设计中的布线技巧

    开关电源PCB设计中的布线技巧关键字:布线 开关电源 走线 一.引言 开关电源是一种电压转换电路,主要的工作内容是升压和降压,广泛应用于现代电子产品.因为开关三极管总是工作在 “开” 和“关” 的状态 ...

  3. 使用Novell.Directory.Ldap.NETStandard在.NET Core中验证AD域账号

    Novell.Directory.Ldap.NETStandard是一个在.NET Core中,既支持Windows平台,又支持Linux平台,进行Windows AD域操作的Nuget包. 首先我们 ...

  4. 在 R 中估计 GARCH 参数存在的问题(基于 rugarch 包)

    目录 在 R 中估计 GARCH 参数存在的问题(基于 rugarch 包) 导论 rugarch 简介 指定一个 \(\text{GARCH}(1, 1)\) 模型 模拟一个 GARCH 过程 拟合 ...

  5. 大数据入门第二十天——scala入门(一)入门与配置

    一.概述 1.什么是scala  Scala是一种多范式的编程语言,其设计的初衷是要集成面向对象编程和函数式编程的各种特性.Scala运行于Java平台(Java虚拟机),并兼容现有的Java程序. ...

  6. 【WPF】给TextBox添上Label

    原文:[WPF]给TextBox添上Label 引言     在客户端开发中,要说出现频率大的控件,必定有TextBox的身影.然而在TextBox的旁边通常得有个基友Label,形影不离.为此,我们 ...

  7. [清华集训2015 Day1]玛里苟斯-[线性基]

    Description Solution 考虑k=1的情况.假设所有数中,第i位为1的数的个数为x,则最后所有的子集异或结果中,第i位为1的个数为$(C_{k}^{1}+C_{k}^{3}+...)$ ...

  8. 11.8 开课二个月零四天 (Jquery取属性值,做全选,去空格)

    1.jquery取复选框的值 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  9. ElasticSearch入门 第五篇:使用C#查询文档

    这是ElasticSearch 2.4 版本系列的第五篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...

  10. 解决Docker容器时区及时间不同步的问题

    前几天在测试应用的功能时,发现存入数据库中的数据create_time或者update_time字段总是错误,其他数据都是正常的,只有关于时间的字段是错误的. 进入linux服务器中查看,也没有任何的 ...