Poj3189_steady cow assignment (Multi-match in the bipartite graph/network Stream)

Source: Internet
Author: User

Solution report

Http://blog.csdn.net/juncoder/article/details/38340447

Question Portal

Question:

B pigsty and N pig. Each pig has a satisfactory value for each pigsty. These pig must be arranged to minimize the difference between the maximum and minimum satisfaction pig.

Ideas:

Multi-match problem of the Bipartite Graph;

Pigsty and source nodes are connected to the edge. The capacity is the pigsty capacity, and the pigsty and sink points are connected to the edge. The capacity is 1;

The line between pigsty and pigsty depends on the satisfactory value range;

The range of the minimum difference between the two points.

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#define inf 99999999using namespace std;int n,m,b,mmap[1030][22],edge[1030][1030],l[1030],c[22];int bfs(){    memset(l,-1,sizeof(l));    l[0]=0;    int i;    queue<int >Q;    Q.push(0);    while(!Q.empty()) {        int u=Q.front();        Q.pop();        for(i=0; i<=m; i++) {            if(edge[u][i]&&l[i]==-1) {                l[i]=l[u]+1;                Q.push(i);            }        }    }    if(l[m]>1)return 1;    return 0;}int dfs(int x,int f){    if(x==m)return f;    int i,a;    for(i=0; i<=m; i++) {        if(l[i]==l[x]+1&&edge[x][i]&&(a=dfs(i,min(f,edge[x][i])))) {            edge[x][i]-=a;            edge[i][x]+=a;            return a;        }    }    l[x]=-1;    return 0;}int dinic(){    int ans=0,a;    while(bfs())        while(a=dfs(0,inf))            ans+=a;    return ans;}int cow(int mid){    int i,j,k;    for(i=1; i<=b-mid+1; i++) {        memset(edge,0,sizeof(edge));        for(j=1; j<=b; j++) {            edge[0][j]=c[j];        }        for(j=1; j<=n; j++) {            for(k=i; k<=i+mid-1; k++) {                edge[mmap[j][k]][j+b]=1;            }            edge[j+b][m]=1;        }        if(dinic()==n)            return 1;    }    return 0;}int main(){    int i,j;    while(~scanf("%d%d",&n,&b)) {        memset(mmap,0,sizeof(mmap));        memset(c,0,sizeof(c));        m=n+b+1;        for(i=1; i<=n; i++) {            for(j=1; j<=b; j++) {                scanf("%d",&mmap[i][j]);            }        }        for(i=1; i<=b; i++) {            scanf("%d",&c[i]);        }        int l=1,r=b,t=-1;        while(l<=r) {            int mid=(l+r)/2;            if(cow(mid)) {                t=mid;                r=mid-1;            } else {                l=mid+1;            }        }        printf("%d\n",t);    }    return 0;}

Steady cow assignment
Time limit:1000 ms   Memory limit:65536 K
Total submissions:5369   Accepted:1845

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 wowould 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 participating 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 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's 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 41 2 3 42 3 1 44 2 3 13 1 2 41 3 4 21 4 2 32 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.