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

Hint

Explanation of the sample: 

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.
 
用最大流求多重匹配

枚举牛棚的最差排名和最好排名(即枚举排名差) ,在这个排名之内牛和这个牛棚建边。

看看每种情况判断是否合法(是否满流),取最小值。

 

Steady Cow Assignment POJ - 3189 (最大流+匹配)_#includeSteady Cow Assignment POJ - 3189 (最大流+匹配)_#include_02
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 200010, INF = 0x7fffffff;
int d[maxn], head[maxn], in[maxn], cur[maxn], w[1100][30], abi[maxn];
int n, m, s, t, minn;
int cnt = 0;

struct node
{
    int u, v, c, next;
}Node[2*maxn];

void add_(int u, int v, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].c = c;
    Node[cnt].next = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int c)
{
    add_(u, v, c);
    add_(v, u, 0);
}

bool bfs()
{
    queue<int> Q;
    mem(d, 0);
    Q.push(s);
    d[s] = 1;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for(int i=head[u]; i!=-1; i=Node[i].next)
        {
            node e = Node[i];
            if(!d[e.v] && e.c > 0)
            {
                d[e.v] = d[e.u] + 1;
                Q.push(e.v);
                if(e.v == t) return 1;
            }
        }
    }
    return d[t] != 0;
}

int dfs(int u, int cap)
{
    int ret = 0, V;
    if(u == t || cap == 0)
        return cap;
    for(int &i=cur[u]; i!=-1; i=Node[i].next)
    {
        node e = Node[i];
        if(d[e.v] == d[u] +  1 && e.c > 0)
        {
            int V = dfs(e.v, min(cap, e.c));
            Node[i].c -= V;
            Node[i^1].c += V;
            ret += V;
            cap -= V;
            if(cap == 0) break;
        }
    }
    if(cap > 0) d[u] = -1;
    return ret;
}

int dinic(int u)
{
    int ans = 0;
    while(bfs())
    {
        memcpy(cur, head, sizeof(head));
        ans += dfs(u, INF);
    }
    return ans;
}

int main()
{
    scanf("%d%d", &n, &m);
    minn = INF;
    s = 0, t = n + m + 1;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
        {
            scanf("%d",&w[i][j]);
        }
    for(int i=1; i<=m; i++)
        scanf("%d",&abi[i]);
    for(int h=1; h<=m; h++)
    {
        for(int i=h; i<=m; i++)
        {
            mem(head, -1);
            cnt = 0;
            for(int k=1; k<=n; k++)
                add(s, k, 1);
            for(int k=1; k<=m; k++)
                add(n+k, t, abi[k]);
            for(int j=1; j<=n; j++)
            {
                for(int k=h; k<=i; k++)
                    add(j, n+w[j][k], 1);
            }
            if(dinic(s) == n)
            {
               minn = min(minn, i-h+1);
            }
        }
    }
    printf("%d\n",minn);

    return 0;
}
View Code

 

自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。