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 ...
随机推荐
- Metasploit 读书笔记-神器Meterpreter
一、基本命令 截屏 screenshot 2.获取系统平台信息 sysinfo 3.进程信息 ps 4.获取键盘记录 查看进程信息ps--migrate将会话迁移至explorer.exe进程空间中- ...
- webservice服务及客户端 编程 - 入门
开发工具 eclipse 建立一个简单的webservice服务 1 创建服务 (1)创建一个 java项目(java project)或 web项目(Dynamic web project) (2) ...
- B - Factors of Factorial
Problem Statement You are given an integer N. Find the number of the positive divisors of N!, modulo ...
- luogu P3811线性求逆元
首先扩O:T了一个点(因为上界松),83分. #include <cstdio> using namespace std; int n, p; void exgcd(int a, int ...
- nginx的worker_processes优化
nginx的worker_processes参数来源: http://bbs.linuxtone.org/thread-1062-1-1.html分享一:搜索到原作者的话:As a general r ...
- Java Web之数据库连接池
数据库连接池 一.数据库连接池 1. 数据库连接池就是存放数据库连接(Connection)的集合 2. 我们获取一个数据库连接是一个相对很麻烦的过程,如果我们获取一个数据库连接,使用一次以后就给它关 ...
- phantomjs截图中文网站网页页面乱码,安装字体解决
用phantomjs去截取中文页面的网站可能会出现乱码的情况,也就是截图中中文的位置全是方框. 解决办法就是安装字体. 在centos中执行:yum install bitmap-fonts bitm ...
- Windows自动化---模拟鼠标键盘
1.PyUserInput(不推荐) python2可以使用PyUserInput库:(不推荐) 支持最基础的鼠标,键盘操作,可以剪贴. 安装的时候:pip install PyUserInput 需 ...
- Flask&&人工智能AI --5 Flask-session、WTForms、数据库连接池、Websocket
未完待续.... DButils 什么是数据库连接池 数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个:释放空闲时间超过最大空闲时间的数据库 ...
- buildKibanaServerUrl
private String buildKibanaServerUrl(DiscountIndexMailData mailData,Statistic stat,String failureCaus ...