POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-3189
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6979 | Accepted: 2418 |
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.
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);
}
}
POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分的更多相关文章
- POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)
解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪.每头猪对每一个猪圈有一个惬意值.要求安排这 ...
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Steady Cow Assignment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2112 Optimal Milking Time Limit: 2000MS Memory Limit: 30000K T ...
- POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-2289 Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 6 ...
- POJ3189 Steady Cow Assignment
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6817 Accepted: ...
- hdu3605 Escape 二分图多重匹配/最大流
2012 If this is the end of the world how to do? I do not know how. But now scientists have found tha ...
- Steady Cow Assignment POJ - 3189 (最大流+匹配)
Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which ...
- Steady Cow Assignment---poj3189(多重匹配+二分)
题目链接:http://poj.org/problem?id=3189 题意:有n头牛,B个牛棚,每头牛对牛棚都有一个喜欢度,接下来输入N*B的矩阵第i行第j列的数x表示:第i头牛第j喜欢的是x; 第 ...
- POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment
这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...
随机推荐
- jmeter给cookie设置sessionId避免其他脚本多次登录
1.相关知识: http头部可以设置:浏览器显示内容类型,如content-type:text/html http头部可以存放:浏览器的cookie信息——cookie是对用户身份进行判断的内容 ht ...
- 通过一个用户管理实例学习路由react-router-dom知识
我们通过一个用户管理实例来学习react-router-dom 这个实例包括9个小组件 App.js 引入组件 Home.js 首页组件 User.js 用户管理组件 - UserList.js 用 ...
- mysql与时间有关的查询
date(str)函数可以返回str中形如"1997-05-26"格式的日期,str要是合法的日期的表达式,如2008-08-08 22:20:46 时间是可以比较大小的,例如: ...
- Back弹出AlertDialog
package com.pingyijinren.helloworld.activity; import android.content.DialogInterface; import android ...
- Java使用IText(VM模版)导出PDF
Java使用IText(VM模版)导出PDF: public String createPDF(ProjectManageBase projectManageBase) { Map map = new ...
- ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和
链接 https://nanti.jisuanke.com/t/31456 参考题解 https://blog.csdn.net/ftx456789/article/details/82590044 ...
- 检测socket链接是否断开
[解决方案] 1. 发送重试,由业务完成. 因为club_l5的send接口不会保留用户发送的内容,在recv失败的情况下,用户发送的数据已经丢失,所以只能由业务进行重试. 结论: ...
- intelliJ IDEA工具快捷键
F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 Step Ove ...
- 【Nginx】基本数据结构
整型的封装 typedef intptr_t ngx_int _t;//有符号整型 typedef uintptr_t ngx_uint_t;//无符号整型 字符串的封装 typedef struct ...
- android多个fragment返回键层层返回
在FragmentActivity的fragment跳转的时候加入到执行栈. public void switchFrag(BaseFragment to) { getSupportFragmentM ...