POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
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
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
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.
#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——————【多重匹配、二分枚举区间长度】的更多相关文章
- Poj 3189 Steady Cow Assignment (多重匹配)
题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...
- POJ 3189 Steady Cow Assignment 【二分】+【多重匹配】
<题目链接> 题目大意: 有n头牛,m个牛棚,每个牛棚都有一定的容量(就是最多能装多少只牛),然后每只牛对每个牛棚的喜好度不同(就是所有牛圈在每个牛心中都有一个排名),然后要求所有的牛都进 ...
- POJ 3189 Steady Cow Assignment
题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小. 题目输入: 首先是两个 ...
- POJ 3189 Steady Cow Assignment【网络流】
题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小. 牛棚个数最多为20,那么直 ...
- POJ 2112 Optimal Milking(Floyd+多重匹配+二分枚举)
题意:有K台挤奶机,C头奶牛,每个挤奶机每天只能为M头奶牛服务,下面给的K+C的矩阵,是形容相互之间的距离,求出来走最远的那头奶牛要走多远 输入数据: 第一行三个数 K, C, M 接下来是 ...
- HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...
- POJ3189:Steady Cow Assignment(二分+二分图多重匹配)
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7482 Accepted: ...
- POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65 ...
- POJ 2289(多重匹配+二分)
POJ 2289(多重匹配+二分) 把n个人,分到m个组中.题目给出每一个人可以被分到的那些组.要求分配完毕后,最大的那一个组的人数最小. 用二分查找来枚举. #include<iostream ...
随机推荐
- c#操作json 使用JavaScriptSerializer
需要引用:System.Web.Extensions /// <summary> /// json的信息.保证定义的变量和json的字段一样(也可以使用struct) /// </s ...
- 洛谷P2526 [SHOI2001]小狗散步(二分图匹配)
题目背景 Grant喜欢带着他的小狗Pandog散步.Grant以一定的速度沿着固定路线走,该路线可能自交.Pandog喜欢游览沿途的景点,不过会在给定的N个点和主人相遇.小狗和主人同时从(X1,Y1 ...
- 最短路【bzoj2464】: 中山市选[2009]小明的游戏
2464: 中山市选[2009]小明的游戏 Description 小明最近喜欢玩一个游戏.给定一个n * m的棋盘,上面有两种格子#和@.游戏的规则很简单:给定一个起始位置和一个目标位置,小明每一步 ...
- LCA 【bzoj 4281】 [ONTAK2015]Związek Harcerstwa Bajtockiego
[bzoj 4281] [ONTAK2015]Związek Harcerstwa Bajtockiego Description 给定一棵有n个点的无根树,相邻的点之间的距离为1,一开始你位于m点. ...
- [比赛|考试]nowcoder NOIPpj组第二场
nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...
- PHP删除目录下包含某个字符串的全部文件
//获取全部的路径 function tree(&$arr_file, $directory, $dir_name='') { $mydir = dir($directory); whi ...
- Hibernate上传数据到数据库,从数据库读取数据到本地模板代码
1.Hibernate上传数据到数据库: //创建一个session对象 Session session1=HibernateTools.getSession(); //Fenciresult数据库表 ...
- Django 12 中间件、上下文处理器和admin后台
Django 12 中间件.上下文处理器和admin后台 一.中间件 #Django中间件 (Middleware) # 一个轻量级.底层的“插件”系统,可以介入Django的请求和响应处理过程,修改 ...
- jQuery中animate()方法以及$('body').animate({"scrollTop":top})不被Firefox支持问题的解决
$("body").animate({"scrollTop":top}): 只被chrome支持,而不被Firefox支持 $("html" ...
- 兼容IE,chrome,ff的设为首页、加入收藏及保存到桌面
// JavaScript Document// 加入收藏 < a onclick="AddFavorite(window.location,document.title)" ...