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

1A~

题意:n个牛找B个槽子,对于每个槽子有不同的满意度,槽子有容量限制,让所有牛找到自己的槽子,满意差距尽可能少

做法:枚举差距,再枚举范围,没了==

#include <iostream>
#include<cstdio>
using namespace std;
const int mm=555555;
const int mn=1555;
const int oo=1000000000;
int node,src,dest,edge;
int reach[mm],flow[mm],next[mm];
int head[mn],work[mn],dis[mn],q[mn];
inline int min(int a,int b)
{
    return a<b?a:b;
}
inline void prepare(int _node,int _src,int _dest)
{
    node=_node,src=_src,dest=_dest;
    for(int i=0;i<node;++i)head[i]=-1;
    edge=0;
}
inline void addedge(int u,int v,int c)
{
    reach[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;
    reach[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;
}
bool Dinic_bfs()
{
    int i,u,v,l,r=0;
    for(i=0;i<node;++i)dis[i]=-1;
    dis[q[r++]=src]=0;
    for(l=0;l<r;++l)
        for(i=head[u=q[l]];i>=0;i=next[i])
            if(flow[i]&&dis[v=reach[i]]<0)
            {
                dis[q[r++]=v]=dis[u]+1;
                if(v==dest)return 1;
            }
    return 0;
}
int Dinic_dfs(int u,int exp)
{
    if(u==dest)return exp;
    for(int &i=work[u],v,tmp;i>=0;i=next[i])
        if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)
        {
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            return tmp;
        }
    return 0;
}
int Dinic_flow()
{
    int i,ret=0,delta;
    while(Dinic_bfs())
    {
        for(i=0;i<node;++i)work[i]=head[i];
        while((delta=Dinic_dfs(src,oo)))ret+=delta;
    }
    return ret;
}
///============================================================
int goal[1009][30];
int lim[1009];
int n,b;
int main()
{
    //freopen("cin.txt","r",stdin);
    while(~scanf("%d%d",&n,&b))
    {
        for(int i=1;i<=n;i++)
        for(int j=1;j<=b;j++)
        {
            int a;
            scanf("%d",&a);
            goal[i][a]=j;
        }
        for(int i=1;i<=b;i++)scanf("%d",&lim[i]);
        bool fl=1;
        for(int len=0;len<=b&&fl;len++)
        {
            for(int st=1;st<=b-len&&fl;st++)
            {
                prepare(2+n+b,0,n+b+1);
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=b;j++)
                        if(goal[i][j]>=st&&goal[i][j]<=st+len)
                            addedge(i,j+n,1);
                for(int j=1;j<=b;j++)addedge(j+n,dest,lim[j]);
                for(int i=1;i<=n;i++)addedge(0,i,1);
                if(n==Dinic_flow())
                {
                    fl=0;
                    printf("%d\n",1+len);
                }
            }
        }

    }
    return 0;
}