题目链接:https://vjudge.net/problem/POJ-3189

Steady Cow Assignment
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6979   Accepted: 2418

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

6 4
1 2 3 4
2 3 1 4
4 2 3 1
3 1 2 4
1 3 4 2
1 4 2 3
2 1 3 2

Sample Output

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个牲棚里住。每头牛对每个牲棚都有一个好感度排名。主人为了使得这些牛尽可能满意,规定了获得最低好感度的牛和获得最高好感度的牛的好感度差值最小(即好感度跨度最小)。

1.二分跨度。然后对于每个跨度,枚举最低好感度(最高好感度也就可以求出),然后开始建图:如果某头牛对某个牲棚的好感度在这个范围内,则连上边;否则不连。

2.用二分图多重匹配或者最大流,求出是否每头牛都可以被安排到某个牲棚中。如果可以,则缩小跨度,否则增大跨度。

多重匹配:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXM = +;
const int MAXN = 1e3+; int uN, vN, Rank[MAXN][MAXM];
int num[MAXM], linker[MAXM][MAXN];
bool g[MAXN][MAXM], used[MAXM]; bool dfs(int u)
{
for(int v = ; v<=vN; v++)
if(g[u][v] && !used[v])
{
used[v] = true;
if(linker[v][]<num[v])
{
linker[v][++linker[v][]] = u;
return true;
}
for(int i = ; i<=num[v]; i++)
if(dfs(linker[v][i]))
{
linker[v][i] = u;
return true;
}
}
return false;
} bool hungary()
{
for(int i = ; i<=vN; i++)
linker[i][] = ;
for(int u = ; u<=uN; u++)
{
memset(used, false, sizeof(used));
if(!dfs(u)) return false;
}
return true;
} bool test(int mid)
{
for(int down = ; down<=vN-mid+; down++)
{
int up = down+mid-;
memset(g, false, sizeof(g));
for(int i = ; i<=uN; i++)
for(int j = down; j<=up; j++)
g[i][Rank[i][j]] = true; if(hungary()) return true;
}
return false;
} int main()
{
while(scanf("%d%d", &uN, &vN)!=EOF)
{
for(int i = ; i<=uN; i++)
for(int j = ; j<=vN; j++)
scanf("%d", &Rank[i][j]); for(int i = ; i<=vN; i++)
scanf("%d", &num[i]); int l = , r = vN;
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}

最大流:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXM = +;
const int MAXN = 2e3+; struct Edge
{
int to, next, cap, flow;
}edge[MAXN*MAXN];
int tot, head[MAXN]; int uN, vN, Rank[MAXN][MAXM], num[MAXM];
int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN]; void add(int u, int v, int w)
{
edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = ;
edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].flow = ;
edge[tot].next = head[v]; head[v] = tot++;
} int sap(int start, int end, int nodenum)
{
memset(dep, , sizeof(dep));
memset(gap, , sizeof(gap));
memcpy(cur, head, sizeof(head));
int u = pre[start] = start, maxflow = ,aug = INF;
gap[] = nodenum;
while(dep[start]<nodenum)
{
loop:
for(int i = cur[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap-edge[i].flow && dep[u]==dep[v]+)
{
aug = min(aug, edge[i].cap-edge[i].flow);
pre[v] = u;
cur[u] = i;
u = v;
if(v==end)
{
maxflow += aug;
for(u = pre[u]; v!=start; v = u,u = pre[u])
{
edge[cur[u]].flow += aug;
edge[cur[u]^].flow -= aug;
}
aug = INF;
}
goto loop;
}
}
int mindis = nodenum;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v=edge[i].to;
if(edge[i].cap-edge[i].flow && mindis>dep[v])
{
cur[u] = i;
mindis = dep[v];
}
}
if((--gap[dep[u]])==)break;
gap[dep[u]=mindis+]++;
u = pre[u];
}
return maxflow;
} bool test(int mid)
{
for(int down = ; down<=vN-mid+; down++)
{
tot = ;
memset(head, -, sizeof(head));
for(int i = ; i<=uN; i++)
{
add(, i, );
int up = down+mid-;
for(int j = down; j<=up; j++)
add(i, uN+Rank[i][j], );
}
for(int i = ; i<=vN; i++)
add(uN+i, uN+vN+, num[i]); int maxflow = sap(, uN+vN+, uN+vN+);
if(maxflow==uN) return true;
}
return false;
} int main()
{
while(scanf("%d%d", &uN, &vN)!=EOF)
{
for(int i = ; i<=uN; i++)
for(int j = ; j<=vN; j++)
scanf("%d", &Rank[i][j]); for(int i = ; i<=vN; i++)
scanf("%d", &num[i]); int l = , r = vN;
while(l<=r)
{
int mid = (l+r)>>;
if(test(mid))
r = mid - ;
else
l = mid + ;
}
printf("%d\n", l);
}
}

最新文章

  1. 【开源】.Net 分布式服务中心
  2. ssm控制输出sql(二)
  3. [LeetCode]题解(python):125 Valid Palindrome
  4. ADS报错 Warning&#160;:&#160;L6301W:Could&#160;not&#160;find&#160;file&#160;C:\Program&#160;Files&#160;.&#160;Error&#160;:&#160;L6218&#160;:&#160;Undefined&#160;symbol&#160;......
  5. 【海岛帝国系列赛】No.4 海岛帝国:LYF的太空运输站
  6. XML文件操作学习(一)
  7. 【M18】分期摊还预期的计算成本
  8. dup和dup2函数
  9. ASP.NET 将DataTable解析成JSON简介
  10. 【操作系统】进程间通信(C#)
  11. 修ecshop品牌筛选以LOGO图片形式显示
  12. Linux笔记(十一) - 文件系统管理
  13. 小程序中通过判断id来删除数据,当数据长度为0时,显示隐藏部分(交流QQ群:604788754)
  14. face detection[Face R-CNN]
  15. Go linux 实践 1
  16. linux服务器规格查看
  17. 【代码笔记】iOS-cell折叠
  18. mysql my.cnf 或my.ini配置文件参数解释(转):
  19. [PHP] Ubuntu 16.10 开启PHP错误提示
  20. Jquery实现$.fn.extend和$.extend函数

热门文章

  1. Python requests.post方法中data与json参数区别
  2. vs2017编译boost 1.70.0
  3. 大数据学习——linux常用命令(二)四
  4. asp.net mvc数据验证
  5. Codeforces Round #211 (Div. 2)-D. Renting Bikes,二分!感谢队友出思路!
  6. PTA 04-树6 Complete Binary Search Tree (30分)
  7. Bzoj 2726 SDOI 任务安排
  8. CF788E:New task
  9. 进程&amp;进程池
  10. APP后端处理表情的一些技巧