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

Description

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.

Source

————————————————————————————

题目的意思是:有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度。棚子有最大容量了,现在要给每个奶牛安家,找一个奶牛喜爱程度差值最小的方案问喜爱程度的区间最小为多大?

思路:尺取枚举区间端点,二分图多重匹配验证

注意:2~N+1行每行的每个数x不是指i对j的喜爱程度为x而是i对x的喜爱程度为j

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <set>
  10. #include <stack>
  11. #include <map>
  12. #include <climits>
  13. using namespace std;
  14.  
  15. #define LL long long
  16. const int INF = 0x3f3f3f3f;
  17. const int MAXN=1005;
  18. int uN,vN; //u,v数目
  19. int g[MAXN][MAXN];
  20. int linker[MAXN][MAXN];
  21. bool used[MAXN];
  22. int linknum[MAXN];
  23. int cap[MAXN];
  24. int mp[MAXN][MAXN];
  25. int L,R;
  26.  
  27. bool dfs(int u)
  28. {
  29. int v;
  30. for(v=1; v<=vN; v++)
  31. if(mp[u][v]<=R&&mp[u][v]>=L&&!used[v])
  32. {
  33. used[v]=true;
  34. if(linknum[v]<cap[v])
  35. {
  36. linker[v][++linknum[v]]=u;
  37. return true;
  38. }
  39. for(int i=1; i<=cap[v]; i++)
  40. if(dfs(linker[v][i]))
  41. {
  42. linker[v][i]=u;
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48.  
  49. int hungary()
  50. {
  51. int res=0;
  52. int u;
  53. memset(linknum,0,sizeof linknum);
  54. memset(linker,-1,sizeof linker);
  55. for(u=1; u<=uN; u++)
  56. {
  57. memset(used,0,sizeof used);
  58. if(dfs(u)) res++;
  59. }
  60. return res;
  61. }
  62.  
  63. int main()
  64. {
  65. int n,m,k,x;
  66. while(~scanf("%d%d",&uN,&vN))
  67. {
  68. for(int i=1; i<=uN; i++)
  69. for(int j=1; j<=vN; j++)
  70. {
  71. scanf("%d",&x);
  72. mp[i][x]=j;
  73. }
  74.  
  75. for(int i=1; i<=vN; i++)
  76. scanf("%d",&cap[i]);
  77. L= R = 1;
  78. int ans = INF;
  79. while(L <= R && R <= vN)
  80. {
  81. if(hungary()==uN)
  82. {
  83. ans=min(ans,R-L+1);
  84. L++;
  85. }
  86. else
  87. R++;
  88. }
  89. printf("%d\n",ans);
  90. }
  91. return 0;
  92. }

  

POJ3189 Steady Cow Assignment的更多相关文章

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

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

  2. POJ3189 Steady Cow Assignment(最大流)

    题目大概说,有n头牛和b块草地,每头牛心中分别对每块草地都有排名,草地在牛中排名越高牛安排在那的幸福度就越小(...),每块草地都能容纳一定数量的牛.现在要给这n头牛分配草地,牛中的幸福度最大与幸福度 ...

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

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

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

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

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

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

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

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

  7. Steady Cow Assignment POJ - 3189 (最大流+匹配)

    Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which ...

  8. P2857 [USACO06FEB]稳定奶牛分配Steady Cow Assignment

    题目描述 Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns ...

  9. Steady Cow Assignment

    poj3189:http://poj.org/problem?id=3189 题意:这一题的题意.我看了很长时间才弄懂.就是给你n头牛,m个牛棚,每个牛对每一个牛棚会有一个满值,第i行第j个数表示的是 ...

随机推荐

  1. istio-jaeger-spring boot调用链配置

    istio-jaeger-spring boot调用链配置 虽然,istio ingress controller已经生成了jaeger 记录所需要的信息,但是多个分布式之间没法清晰记录相互之间的依赖 ...

  2. HTML知识基础

    HTML 超文本标记语言(Hyper  Text  Markup Language):是一种用于创建网页的标准标记语言. Hyper  Text:指具有交互功能文本. Markup Language: ...

  3. Linux Centos7 虚拟机上网设置

    一.Windows下网卡,共享上网模式 二.虚拟机VMnet8的网卡模 三.Linux 下配置网卡 命令: vi /etc/sysconfig/network-scripts/ifcfg-ens33 ...

  4. 找不到或无法加载主类(Could not find or load main class )

    在Linux环境下,写了一个简单的java程序,通过javac编译成class文件,然后用java 运行的时候,报了这个错误, 搜了一下,可能是classpath的问题,所以用echo $CLASSP ...

  5. Ubuntu安装bcmath扩展

    sudo apt-get install php-bcmath

  6. Data Dictionary 数据字典

    数据字典是一种通用的程序设计方法.可以认为,不论什么程序,都是为了处理一定的主体,这里的主体可能是人员.商品(超子).网页.接口.数据库表.甚至需求分析等等.当主体有很多的属性,每种属性有很多的取值, ...

  7. merge_关系

    create(olive:Person{chau:"Bill White",name:"Olive Stone",bornin:"New York&q ...

  8. 9款原型设计工具与Sketch的强强组合,轻松构建交互原型!

    原型设计的发展历史经历了纸上原型.静态线框设计.到现在的可交互式原型.作为设计过程中最初始的阶段,设计师们对原型设计的要求也越来越高.因此,如今的原型设计工具格局也发生了很大的变化. Sketch对于 ...

  9. Previous Permutation

    Similar to next permutation, the steps as follow: 1) Find k in the increasing suffix such that nums[ ...

  10. socket的同步异步的性能差别,以及listen的参数backlog

    先说listen的参数backlog,同步系统中分别设置为5,512,1024的跑分情况 跑分工具apache的ab,参数为:ab -n50000 -c300 backlog=5跑分结果 Reques ...