Steady Cow Assignment

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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.

 
 
题目大意:给你N表示有N头牛,B表示有B个牛棚。下边N*B的矩阵A,N行,表示N头牛,B列表示喜爱排名从高到低。那么A(i,j)表示第i头牛喜欢排名为j的是哪个牛棚,最后一行为每个牛棚能装多少头牛。数据保证N头牛一定能都安排到牛棚中。现在让你求最小的排名区间长度。
 
解题思路:一对多,多重匹配。二分枚举区间长度,同时枚举排名左端点和排名右端点。如果匹配成功,说明这个区间长度是可以的,记录答案,同时向小逼近,如果不成功,向大逼近。
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1100;
int Map[maxn][maxn];
int linker[maxn][maxn], used[maxn], cap[maxn];
bool dfs(int u,int rn,int st,int en){
for(int v = 1; v <= rn; v++){
if(used[v] ){
continue;
}
if(Map[u][v] > en || Map[u][v] < st){
continue;
}
used[v] = 1;
if(linker[v][0] < cap[v]){
linker[v][++linker[v][0]] = u;
return true;
}else{
for(int j = 1; j <= linker[v][0]; j++){
if(dfs(linker[v][j],rn,st,en)){
linker[v][j] = u;
return true;
}
}
}
}
return false;
}
bool Hungary(int ln,int rn,int mid){
int en ;
for(int st = 1; st <= rn -mid + 1; st++){
en = st + mid - 1;
int ret = 0;
for(int i = 0; i <= rn; i++){
linker[i][0] = 0;
}
for(int i = 1; i <= ln; i++){
memset(used,0,sizeof(used));
if(dfs(i,rn,st,en)){
ret++;
}
}
if(ln == ret){
return true;
}
}
return false;
}
int main(){
int N, B;
int matrix[1200][50];
while(scanf("%d%d",&N,&B)!=EOF){
int c;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= B; j++){
scanf("%d",&c);
Map[i][c] = j;
}
}
for(int i = 1; i <= B; i++){
scanf("%d",&cap[i]);
}
int l = 1, r = B, ans;
while(l <= r){
int mid = (l+r)/2;
if(Hungary(N,B,mid)){
r = mid -1;
ans = mid;
}else{
l = mid + 1;
}
}
printf("%d\n",ans);
}
return 0;
}

  

还有一种最开始想到的,每次枚举,每次建图,而不是限制区间,时间没有上面的快,但是更好理解。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int INF = 9999999;
const int maxn = 1100;
int Map[maxn][maxn];
int linker[maxn][maxn], used[maxn], cap[maxn];
bool dfs(int u,int rn){
for(int v = 1; v <= rn; v++){
if(used[v] || !Map[u][v]){
continue;
}
used[v] = 1;
if(linker[v][0] < cap[v]){
linker[v][++linker[v][0]] = u;
return true;
}else{
for(int j = 1; j <= linker[v][0]; j++){
if(dfs(linker[v][j],rn)){
linker[v][j] = u;
return true;
}
}
}
}
return false;
}
bool Hungary(int ln,int rn){
int ret = 0;
for(int i = 0; i <= rn; i++){
linker[i][0] = 0;
}
for(int i = 1; i <= ln; i++){
memset(used,0,sizeof(used));
if(dfs(i,rn)){
ret++;
}
}
if(ln == ret){
return true;
}
return false;
}
int main(){
int N, B;
int matrix[1200][50];
while(scanf("%d%d",&N,&B)!=EOF){
for(int i = 1; i <= N; i++){
for(int j = 1; j <= B; j++){
scanf("%d",&matrix[i][j]);
}
}
for(int i = 1; i <= B; i++){
scanf("%d",&cap[i]);
}
int l = 1, r = B, ans;
while(l <= r){
int mid = (l+r)/2;
int flag = 0;
for(int i = 1; i <= B - mid + 1; i++){
memset(Map,0,sizeof(Map));
for(int j = 1; j <= N; j++){
for(int k = i; k < i + mid; k++){
Map[j][matrix[j][k]] = 1;
}
}
if(Hungary(N,B)){
flag = 1; break;
}
}
if(flag){
r = mid - 1;
ans = mid;
}else{
l = mid + 1;
}
}
printf("%d\n",ans); }
return 0;
}

  

 

POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】的更多相关文章

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

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

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

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

  3. POJ 3189 Steady Cow Assignment

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

  4. POJ 3189 Steady Cow Assignment【网络流】

    题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小. 牛棚个数最多为20,那么直 ...

  5. POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)

    题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远   输入数据: 第一行三个数 K, C, M  接下来是   ...

  6. HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...

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

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

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

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

  9. POJ 2289(多重匹配+二分)

    POJ 2289(多重匹配+二分) 把n个人,分到m个组中.题目给出每一个人可以被分到的那些组.要求分配完毕后,最大的那一个组的人数最小. 用二分查找来枚举. #include<iostream ...

随机推荐

  1. 爬取腾讯课堂IT-互联网分类的的课程信息存入csv文件

    标签(空格分隔): python from urllib.request import urlopen from bs4 import BeautifulSoup #获取IT-互联网分类每页的课程的链 ...

  2. Flask 程序的基本结构

    1.初始化 所有Flask程序都必须创建一个程序实例.web服务器使用一种名为Web服务器网关借口的协议,把接收自客户端的所有请求都转交给这个对象处理. from flask import Flask ...

  3. forEach时候删除数组某一属性项,使用splice容易出现问题

    第一次forEach循环,index是0,item是1 ,arr是[1,1,2], if条件成立,使用splice最终的arr是[1,2] 第二次循环,index是1,item是2,arr是[1,2] ...

  4. Exadata LVM snapshot备份失败

    一台X4-2 的计算节点进行image升级,在正式升级之前利用LVM snapshot备份操作系统时备份失败,并且报大量IO错误,提示无法找到LVM snapshot的挂载点.检查文件系统状态: [r ...

  5. SprimgMVC学习笔记(七)—— 上传图片

    一.配置虚拟目录 在tomcat上配置图片虚拟目录,在tomcat下conf/server.xml中添加: <Context docBase="D:\upload\temp" ...

  6. Codeforces-B-Divisors of Two Integers(思维技巧)

    Recently you have received two positive integer numbers xx and yy. You forgot them, but you remember ...

  7. 浅谈c语言的线性表的基本操作———基于严蔚敏的数据结构(c语言版)

    主要讲的是线性表的创建,插入及删除: 0. 线性表的建立,对于这类操作主要是利用了结构体的性质,对于定义的线性表的特性主要有三点:首先 Typedef struct { ElemType   *ele ...

  8. LDAP环境搭建 OpenLDAP和phpLDAPadmin -- yum版

      前言: 前两天公司要求做一个使用LDAP和Kerberos做一个认证授权系统,然后开始学习LDAP相关知识,期间找了不少博客按照步骤来安装,可是很多博客在配置的时候,都会遇到安装过程中一两个问题卡 ...

  9. classloader 学习

    classloader就是把类文件加载到jvm中供虚拟机使用,先看一个magic小例子: 首先,我定义一个alex/vicky包,然后在这个包内定义一个接口: public interfaceISer ...

  10. Problem08 输入数字求和

    题目:求s=a+aa+aaa+aaaa+aa...a 的值,其中a 是一个数字.例如2+22+222+2222+22222(此时共有5 个数相加),几个数相加有键盘控制. 程序分析:关键是计算出每一项 ...