Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy.

FJ would like to rearrange the cows such that the cows are as equally happy as possible, even if that means all the cows hate their assigned barn.

Each cow gives FJ the order in which she prefers the barns. A cow's happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that no barn's capacity is exceeded and the size of the range (i.e., one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.

Input

Line 1: Two space-separated integers, N and B

Lines 2..N+1: Each line contains B space-separated integers which are exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow i's top-choice barn, the second integer on that line is the number of the i'th cow's second-choice barn, and so on.

Line N+2: B space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so on. The sum of these numbers is guaranteed to be at least N.

Output

Line 1: One integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the endpoints.

Sample Input

  1. 6 4
  2. 1 2 3 4
  3. 2 3 1 4
  4. 4 2 3 1
  5. 3 1 2 4
  6. 1 3 4 2
  7. 1 4 2 3
  8. 2 1 3 2

Sample Output

  1. 2

Hint

Explanation of the sample:

Each cow can be assigned to her first or second choice: barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, and barn 4 gets cows 3 and 6.

 
用最大流求多重匹配  https://www.cnblogs.com/WTSRUVF/p/9312104.html

枚举牛棚的最差排名和最好排名(即枚举排名差) ,在这个排名之内牛和这个牛棚建边。

看看每种情况判断是否合法(是否满流),取最小值。

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

Steady Cow Assignment POJ - 3189 (最大流+匹配)的更多相关文章

  1. O - Steady Cow Assignment - POJ 3189(多重匹配+枚举)

    题意:有N头奶牛,M个牛棚,每个牛棚都有一个容量,并且每个牛对牛棚都有一个好感度,现在重新分配牛棚,并且使好感觉最大的和最小的差值最小. 分析:好感度貌似不多,看起来可以枚举一下的样子,先试一下把 注 ...

  2. POJ3189:Steady Cow Assignment(二分+二分图多重匹配)

    Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7482   Accepted: ...

  3. POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】

     Steady Cow Assignment Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  4. Poj 3189 Steady Cow Assignment (多重匹配)

    题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...

  5. POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65 ...

  6. POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment

    这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...

  7. POJ3189 Steady Cow Assignment

    Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6817   Accepted:  ...

  8. POJ 3189 Steady Cow Assignment 【二分】+【多重匹配】

    <题目链接> 题目大意: 有n头牛,m个牛棚,每个牛棚都有一定的容量(就是最多能装多少只牛),然后每只牛对每个牛棚的喜好度不同(就是所有牛圈在每个牛心中都有一个排名),然后要求所有的牛都进 ...

  9. POJ 3189 Steady Cow Assignment

    题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小.   题目输入: 首先是两个 ...

随机推荐

  1. day06--元组、字典、集合与关系运算

    今日内容: 1.元组 2.字典 3.集合与关系运算 元组: 用途:记录多个值,当多个值没有改的需求,此时用元组更适合. 定义方式:在()内用逗号分隔开多个任意类型的值. 变量名=tuple('') 切 ...

  2. 解决Win10家庭版没有‘本地用户和组’问题

    今天偶然发现我的win10系统是家庭版,并且没有本地用户和组. 处理方法:将系统升至为win10专业版,然后下载microKMS_v17.02.14做的激活.参考网站 1.打开运行窗口,输入 gped ...

  3. 转自《https安全链接的配置教程:startSSl免费证书申请与nginx的https支持配置》

    一.什么是 SSL 证书,什么是 HTTPS 网站? SSL证书是数字证书的一种,类似于驾驶证.护照和营业执照的电子副本.SSL证书通过在客户端浏览器和Web服务器之间建立一条SSL安全通道(Secu ...

  4. 20155217《网络对抗》Exp08 Web基础

    20155217<网络对抗>Exp08 Web基础 实践内容 Web前端:HTML基础 Web前端:javascipt基础 Web后端:MySQL基础 Web后端:PHP基础 SQL注入 ...

  5. 20155323刘威良 网络对抗 Exp2 后门原理与实践

    20155323 刘威良<网络攻防>Exp2后门原理与实践 实验内容 (1)使用netcat获取主机操作Shell,cron启动 (0.5分) (2)使用socat获取主机操作Shell, ...

  6. spring配置多个事务管理器

    <tx:annotation-driven/> <bean id="transactionManager1" class="org.springfram ...

  7. [穷尽]ADO.NET连接字符串

    微软提供的四种数据库连接方式: System.Data.OleDb.OleDbConnection System.Data.SqlClient.SqlConnection System.Data.Od ...

  8. Microsoft Dynamics CRM 增删改子表汇总子表的某个字段到主表的某个字段(通用插件)

    背景 经常有某个汇总子表的数量到主表的总数量,或者汇总子表的总价到主表的总价这种需求. 传统的做法: 1.就是为每个子表实体单独写成一个插件,但是这样不好复用. 2.主表的汇总字段是汇总货币类型,但是 ...

  9. node基础:文件系统-文件读取

    node的文件读取主要分为同步读取.异步读取,常用API有fs.readFile.fs.readFileSync.还有诸如更底层的fs.read,以及数据流(stream),后面再总结下咯~ 直接上简 ...

  10. JQ_下雪特效

    这是一个jQuery下雪特效.特效的代码如下: <style>body{background:black;color:white}</style><script>/ ...