Poj 3189 Steady Cow Assignment【二分+多重匹配】
来源:互联网 发布:个人考勤软件 编辑:程序博客网 时间:2024/06/05 17:54
Steady Cow Assignment
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 6589
Accepted: 2260
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
USACO 2006 February Gold
题目大意:有n头牛,b个牛棚,接下来一个n*b的一个矩阵,Aij表示第i头牛第j个喜欢的牛棚编号。为了公平起见,尽可能的使得每头牛的喜欢程度相近,使得最终全部牛都有了牛棚之后,使得最高兴的牛(比如是选择了第j个喜欢的牛棚)-最不高兴的牛(比如是选择了第y个喜欢的牛棚)+1尽可能的小(j-y+1)。
思路:
1、因为b是一个比较小的数据范围,那么我们其实可以直接两层for枚举左右区间,确定没头牛可以选择的第几喜欢的牛棚需要在l,r之间。然后进行多重匹配判定,如果所有牛都能匹配到牛棚,那么当前区间就是一个可行区间。维护记录最小值即可。
2、这里可以有一个二分查找的优化,没加优化是260+ms,加了优化之后大约60+ms。
因为随着区间的增大,可以匹配上的牛的数量会随着增加,那么其满足递增性,那么我们一层for枚举左区间,然后二分查找一个右区间,然后进行多重匹配的判定即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;int vis[1500];int a[1500][50];int match[1500][50];int num[1500];int c[1500];int n,m;int find(int u,int l,int r){ for(int i=l;i<=r;i++) { int v=a[u][i]; if(vis[v]==1)continue; vis[v]=1; if(num[v]<c[v]) { match[v][num[v]]=u; num[v]++; return 1; } else { for(int j=0;j<c[v];j++) { if(find(match[v][j],l,r)==1) { match[v][j]=u; return 1; } } } } return 0;}int Slove(int l,int r){ int flag=0; memset(match,-1,sizeof(match)); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); if(find(i,l,r)==1)continue; else { flag=1;break; } } if(flag==1)return 0; else return 1;}int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { scanf("%d",&a[i][j]); } } for(int i=1;i<=m;i++) { scanf("%d",&c[i]); } int ans=0x3f3f3f3f; for(int i=1;i<=m;i++) { int l=i; int r=m; int mid; while(r-l>=0) { int mid=(l+r)/2; if(Slove(i,mid)==1) { r=mid-1; ans=min(ans,(mid-i+1)); } else { l=mid+1; } } } printf("%d\n",ans); }}
- POJ 3189 Steady Cow Assignment(二分+多重匹配)
- poj 3189 Steady Cow Assignment 二分图多重匹配
- POJ 3189 Steady Cow Assignment(二分+多重匹配)
- poj 3189 Steady Cow Assignment 二分图多重匹配
- Poj 3189 Steady Cow Assignment【二分+多重匹配】
- POJ 3189 Steady Cow Assignment (枚举+二分图的多重匹配)
- poj 3189 Steady Cow Assignment(多重匹配or二分最大流)
- POJ 3189--Steady Cow Assignment【二分图多重匹配 && 最大流求解 && 枚举 && 经典】
- poj3189 Steady Cow Assignment --- 多重匹配,二分图匹配解法
- poj3189--Steady Cow Assignment (二分多重匹配)
- POJ 3189 Steady Cow Assignment
- POJ 3189 Steady Cow Assignment
- Steady Cow Assignment POJ - 3189
- POJ 3189 Steady Cow Assignment 二分最大流
- 【POJ】3189 Steady Cow Assignment 二分最大流
- poj 3189 Steady Cow Assignment(二分+最大流)
- TOJ 1671 Steady Cow Assignment -- 多重二分图匹配 + 二分查找
- Steady Cow Assignment(二分图多重匹配+二分)(网络流)
- 摘录的一些调度算法
- C#之静态函数的使用
- css3-flex-ONE
- Linux命令之"cat"
- JDK动态代理
- Poj 3189 Steady Cow Assignment【二分+多重匹配】
- 模仿Flickr 使用GPUImage的实时相机滤镜(iOS源代码)
- 替换字符串中部分字符串,java
- 关于nmap和traceroute在虚拟机里使用的问题
- SpringMVC返回json数据时乱码解决
- 链表------创建删除增加遍历
- java学习例题之数据加密
- leetcode :Binary Search:H-Index IIe(275)
- 【任玉刚】给Android程序员的一些面试建议