Steady Cow Assignment
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6914   Accepted: 2387

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

二分长度+枚举
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1500;
int head[N],tot,S,T;
int q[N],dis[N],n,m;
int mp[N][22],cap[22];
struct node
{
    int next,v,w;
} e[N*100];
void add(int u,int v,int w)
{
    e[tot].v=v;
    e[tot].w=w;
    e[tot].next=head[u];
    head[u]=tot++;
}
bool bfs()
{
    memset(dis,-1,sizeof(dis));
    dis[S]=0;
    int l=0,r=0;
    q[r++]=S;
    while(l<r)
    {
        int u=q[l++];
        for(int i=head[u]; ~i; i=e[i].next)
        {
            int v=e[i].v;
            if(dis[v]==-1&&e[i].w>0)
            {
                q[r++]=v;
                dis[v]=dis[u]+1;
                if(v==T) return true;
            }
        }
    }
    return false;
}
int dfs(int s,int low)
{
    if(s==T||!low) return low;
    int ans=low,a;
    for(int i=head[s]; ~i; i=e[i].next)
    {
        if(e[i].w>0&&dis[e[i].v]==dis[s]+1&&(a=dfs(e[i].v,min(e[i].w,ans))))
        {
            e[i].w-=a;
            e[i^1].w+=a;
            ans-=a;
            if(!ans) return low;
        }
    }
    if(low==ans) dis[s]=-1;
    return low-ans;
}
bool solve(int l,int r)
{
    memset(head,-1,sizeof(head));
    tot=0;
    int ans=0;
    for(int i=1; i<=n; ++i) add(S,i,1),add(i,S,0);
    for(int i=1; i<=n; ++i) for(int j=l; j<=r; ++j) add(i,mp[i][j]+n,1),add(mp[i][j]+n,i,0);
    for(int i=1; i<=m; ++i) add(i+n,T,cap[i]),add(T,i+n,0);
    while(bfs()) ans+=dfs(S,1008611);
    if(ans==n) return true;
    return false;
}
int main()
{
    scanf("%d%d",&n,&m);S=0,T=n+m+1;
    for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) scanf("%d",&mp[i][j]);
    for(int i=1; i<=m; ++i) scanf("%d",&cap[i]);
    int l=1,r=m,ans=m;
    while(l<=r) {
        int mid=(l+r)>>1;
        bool ok=0;
        for(int i=1;i+mid-1<=m;++i) if(solve(i,i+mid-1)) {
            ok=1;
            r=mid-1;
            ans=mid;
        }
        if(!ok) l=mid+1;
    }
    printf("%d\n",ans);
}

枚举 转化为可行性判定问题 网络流 poj3189的更多相关文章

  1. poj2699 转化为可行性判定问题+二分枚举+最大流

    The Maximum Number of Strong Kings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2302 ...

  2. C#枚举转化示例大全,数字或字符串转枚举

    本文重点举例说明C#枚举的用法,数字转化为枚举.枚举转化为数字及其枚举数值的判断,以下是具体的示例: 先举两个简单的例子,然后再详细的举例说明: 字符串转换成枚举:DayOfWeek week=(Da ...

  3. 【算法】【网络流24题】巨坑待填(成功TJ,有时间再填)

    ------------------------------------------------------------------------------------ 17/24 --------- ...

  4. OC开发中运用到的枚举

      一  常见枚举的定义: typedef enum { LOGIN_SUCCESS, USER_NAME, USER_PASSWORD, OLD_LAT, OLD_LNG }FIELD_SAVED; ...

  5. MVC 枚举绑定 DropDownList

    /// <summary> /// 枚举转化下拉列表数据集 /// </summary> /// <param name="type">类型&l ...

  6. codevs2189数字三角形w——最优性转化

    题目:http://codevs.cn/problem/2189/ 通过增加一维,将最优性转化为可行性. 代码如下: #include<iostream> #include<cstd ...

  7. 将枚举转成SelectListItem

    代码如下: /// <summary> /// 将一个枚举转化成一个List<SelectListItem> /// </summary> /// <type ...

  8. spring boot 枚举使用的坑3

    上一篇说到spring boot 使用jackson在枚举enum序列化和反序列化的问题, 再来说说在JPA中实体entity使用枚举的问题. 还是这个枚举: @Getter @AllArgsCons ...

  9. 【UVA1660】Cable TV Network

    题目大意:给定一个 N 个点的无向图,求至少删去多少个点可以使得无向图不连通. 题解:学习到了点边转化思想. 根据网络流的知识可知,一个网络的最小割与网络的最大流相等.不过最小割是图的边集,而本题则是 ...

随机推荐

  1. java 之 enum(枚举)

    推荐博客 http://blog.csdn.net/javazejian/article/details/71333103

  2. 洛谷P3018 [USACO11MAR]树装饰Tree Decoration

    洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...

  3. #Lab0 Environment Building

    清华提供了实验环境的很多选项,具体可以参考README 我选择用虚拟机完成. 一.安装VirtualBox 下载链接 一路next,我的版本是6.0.4. 二.下载虚拟硬盘文件 实验所需的软件都在虚拟 ...

  4. C++编程入门题目--No.2

    题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高 于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提 成7.5%:20万到4 ...

  5. 难道你现在还不知道:C/S和B/S

    随着网络技术的不断发展,各种各样的网络应用程序大爆发.运用最多的架构是基于浏览器+服务器的B/S结构,另一种是基于的 C/S结构. 概述: BS = Browser / Server =浏览器+服务器 ...

  6. 疯子的算法总结10--最小生成树Kruscal

    按照权值排序可得,就有如下顺序: 1. 1-2 1 2. 1-4 2 3. 1-5 2 4. 2-5 3 5. 2-3 4 6. 4-5 4 每次选取最小边泉,判断是否同属一个集合,如果不属于同一集合 ...

  7. 写给Android 混淆小白的快速混淆方法

    为啥子要混淆 简单来说,Android 进行ProGuard,可以起到压缩,混淆,预检,优化的功能,虽然不能说更安全但还是一个不容忽视的环节. 开始混淆第一步 首先在build.gradle 中将混淆 ...

  8. 迁移WPF项目到.NET CORE

    综述 .NET CORE 3.0开始,桌面端支持WPF了.很多.NET FRAMEWORK的项目已经跑了一阵子了,不是很有必要支持.NET CORE,不过最近用一个程序,为了贯彻一些C# 8的特性,需 ...

  9. Java——字节和字符的区别

    字节 1.bit=1  二进制数据0或1 2.byte=8bit  1个字节等于8位 存储空间的基本计量单位 3.一个英文字母=1byte=8bit 1个英文字母是1个字节,也就是8位 4.一个汉字= ...

  10. Jmeter系列(10)- 阶梯加压线程组Stepping Thread Group详解

    如果你想从头学习Jmeter,可以看看这个系列的文章哦 https://www.cnblogs.com/poloyy/category/1746599.html 前言 Stepping Thread ...