hdu 3189(网络流+二分枚举)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6422 | Accepted: 2202 |
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 <queue>
#include <string.h>
#include <math.h>
#include <iostream>
#include <math.h>
using namespace std;
const int N = ;
const int INF = ;
struct Edge{
int v,next;
int w;
}edge[N*N];
struct Cow{
int love[];
}cow[N];
int head[N];
int level[N];
int graph[N][N],dis[N][N],cap[N];
int tot;
void init(){
memset(head,-,sizeof(head));
tot=;
}
void addEdge(int u,int v,int w,int &k){
edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
edge[k].v = u,edge[k].w=,edge[k].next=head[v],head[v]=k++;
}
int BFS(int src,int des){
queue<int >q;
memset(level,,sizeof(level));
level[src]=;
q.push(src);
while(!q.empty()){
int u = q.front();
q.pop();
if(u==des) return ;
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
int w = edge[k].w;
if(level[v]==&&w!=){
level[v]=level[u]+;
q.push(v);
}
}
}
return -;
}
int dfs(int u,int des,int increaseRoad){
if(u==des) return increaseRoad;
int ret=;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
int w = edge[k].w;
if(level[v]==level[u]+&&w!=){
int MIN = min(increaseRoad-ret,w);
w = dfs(v,des,MIN);
if(w>){
edge[k].w -=w;
edge[k^].w+=w;
ret+=w;
if(ret==increaseRoad) return ret;
}else level[v] = -;
}
}
return ret;
}
int Dinic(int src,int des){
int ans = ;
while(BFS(src,des)!=-) ans+=dfs(src,des,INF);
return ans;
}
int n,b,src,des;
int build(int t,int mid){
init();
for(int i=;i<=b;i++){
addEdge(src,i,cap[i],tot);
}
for(int i=;i<=n;i++){
addEdge(i+b,des,,tot);
}
for(int i=t;i<=t+mid-&&i<=b;i++){
for(int j=;j<=n;j++){
addEdge(cow[j].love[i],j+b,,tot);
}
}
return Dinic(src,des);
}
int main(){
scanf("%d%d",&n,&b);
src = ,des = n+b+;
for(int i=;i<=n;i++){
for(int j=;j<=b;j++){
scanf("%d",&cow[i].love[j]);
}
}
for(int i=;i<=b;i++) scanf("%d",&cap[i]);
int l =,r = b;
int ans = b;
while(l<=r){
int mid = (l+r)>>; ///差值
bool flag = false;
for(int i=;i<=b;i++){ ///枚举最小值
if(build(i,mid)==n) {
flag = ;
break;
}
}
if(flag) {
ans = mid;
r = mid-;
}else l = mid+;
}
printf("%d\n",ans);
}
hdu 3189(网络流+二分枚举)的更多相关文章
- 无题II hdu 2236(二分枚举区间)
分析:只需要用二分找一个区间,然后不断枚举这个区间是否可以达到最大匹配,一直二分到答案为止. 代码: =============================================== ...
- hdu 5289 rmp+二分+枚举后界 or单调队列 ****
好题~~ 给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数,枚举后界~~ 又是一种没见过的方法,太弱了/(ㄒoㄒ)/~~ #include <cstdio ...
- HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Steady Cow Assignment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- hdu 5248 序列变换(二分枚举)
Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...
- hdu 4430 二分+枚举
/* 二分+枚举 枚举k会超时,枚举r还要优化,有可能会超64 */ #include<stdio.h> #include<math.h> #define ll __int64 ...
- BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others) ...
- FZU-2216 The Longest Straight (二分枚举)
题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
- hdu 4768 Flyer 二分
思路:由于最多只有一个是奇数,所以二分枚举这个点,每次判断这个点的左边区间段所有点的和作为 二分的依据. 代码如下: #include<iostream> #include<cstd ...
随机推荐
- 【莫队】bzoj4542: [Hnoi2016]大数
挺有意思的,可以仔细体味一下的题:看白了就是莫队板子. Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小 ...
- Python基础教程2-3:以正确的宽度在居中的“盒子”内打印一个句子
代码示例:#获取句子长度sentence = input('Plese input a sentence:')#He's very naughty boyscreen_width =100#获取文本的 ...
- Selenium2启动浏览器且加载插件
一.SELENIUM2启动浏览器 注意: SELENIUM2在启动浏览器时,都是启动一个干净的没有任务 插件及cookies信息的浏览器,即使是你之前的浏览器有设置过代理,到自动化启动时,也是没有代理 ...
- 15年多校第一场七题hdu5294
要做这题,先要明白图的割,说白了就是 为了让原点无法到汇点要删几条边(之所以叫割,就是在图面上切一刀,减掉最小的边是原点和汇点成为两个集合),想到了割先放着一会用. 题中说只有沿最短路走才有可能追上, ...
- [转] 重定向 CORS 跨域请求
非简单请求不可重定向,包括第一个preflight请求和第二个真正的请求都不行. 简单请求可以重定向任意多次,但如需兼容多数浏览器,只可进行一次重定向. 中间服务器应当同样配置相关 CORS 响应头. ...
- Python 协程、IO模型
1.协程(单线程实现并发)2.I/0模型 2.1阻塞I/O 2.2非阻塞I/O 知识点一:协程 协程的目的:是想要在单线程下实现并发(并发看起来是同时运行的) 并发=多个任务间切换+保存状态(正常情况 ...
- netcore命令行部署|跨域问题
1.在hosting中修改发布端口号,如遇见不识别IP则改成*再用命令行运行 { "server.url": "http://*:8089"} 3.给接口开外网 ...
- C#技术点
程序员的基本内功.操作系统,数据结构,网络协议,架构 mysql存储引擎,索引? 分布式技术一致性? 缓存系统/中间件技术/NoSql? 锁与线程切换? 排序,链表,hash_map?
- JDBC 学习笔记(一)—— JDBC 基础
1. 什么是 JDBC JDBC,Java Database Connectivity(Java 数据库连接),是一组执行 SQL 语句的 Java API. JDBC,是 Java SE(Java ...
- WTForms 表单动态验证
class UserDetails(Form): group_id = SelectField(u'Group', coerce=int) def edit_user(request, id): us ...