Cowshed arrangements

Cowshed arrangements

\(Description\)

Farmer John of N (1 <= N <= 1000) were living in cows owned Farm B (1 <= B <= 20 ) in one of a cow. Some cows liked to live in their current bullpen, while others hate to stay any longer in the bullpen where they are now.
FJ endured after complaining several cows, all cows decided to rearrange the bullpen, so that feelings of difference is most dissatisfied with most of the other end cows happy cows minimum, even if it makes all the cows are more depressed.
Each cow regarded FJ after she told the favorability of each sort descending bullpen. Of course, if a cow is placed in the bullpen on the list given her, she will be more depressed. You can think of cows depressed index is the position she was assigned to the bullpen in the list. Cows are preoccupied, they can not tolerate other cows live happily in your favorite cowshed, while they do not like to stay in a cowshed. Each cow can only accommodate a certain number of dairy cows. FJ hope not each bullpen beyond the premise of capacity constraints, the minimum span depressed index of the most depressing and most happy cows.
FJ ask you to help him write a program to calculate the minimum span of depressed index in the end is how much.

\(Input\)

Line 1: contains two space-separated integers N and B, respectively, and the number of cattle barn
first row 2..N + 1: Each line contains B integers separated by spaces, comprising exactly one complete. the integer .B. The i + 1 row of the first integer indicating cows i bullpen favorite number. The second integer represents a list of cows i's in second place, also is her second favorite bullpen. So on and so forth.
Second row N + 2: B contains integers separated by spaces, the number of the i-th cow integer i can accommodate up cows. All cowshed can accommodate the number of cows and at least N.

\(Output\)

Line 1: Output An integer that represents all the cows in the most happy and most depressing cattle depressed index span

\(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

Thinking

When I first saw this problem, and do not know, you can only play table

After the game, watched solution to a problem, sec know! ! !

Half the maximum flow +

Resolve

The questions asked most depressed and most happy cows depressed index of the smallest span, we might happiest violence enumeration span half the index .
Once this is done, we can consider the network flow.
Source: \ (N \) cows , build a super source is connected to each source , flow \ (1 \) .
Meeting Point: \ (B \) a cow , to build a super sink is connected to the each meeting point , the flow rate of the number of cows can be installed under the cow .
then \ (N \) cows in the most depressed and even most happy range ( enumeration can go cowshed)
last run the maximum flow can again \ (AC \) a

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int tot=1,n,m,a[1005][1005],b[10005],d[10005],q[10005],h[100005],cur[100005];

struct edge{
    int to,nxt,z;
}e[500000];
void add(int x,int y,int z)
{
    e[++tot].to=y;
    e[tot].nxt=h[x];
    e[tot].z=z;
    h[x]=tot;
}
void build(int x,int y)
{
    tot=1;
    memset(h,0,sizeof(h));
    for (int i=1;i<=n;i++) add(0,i,1),add(i,0,0);
    for (int i=1;i<=n;i++)
        for (int j=x;j<=y;j++)
            add(i,a[i][j]+n,1),add(a[i][j]+n,i,0);
    for (int i=1;i<=m;i++) add(n+i,n+n+1,b[i]),add(n+n+1,n+i,0);
}
bool bfs(int u)
{
    for(int i=0;i<=n+n+n;i++) cur[i]=h[i]; 
    memset(d,0,sizeof(d));
    memset(q,0,sizeof(q));
    int head=0,tail=0;
    d[u]=1;
    q[++tail]=u;
    while (head<tail)
    {
        head++;
        for (int i=h[q[head]];i;i=e[i].nxt)
        {
            int v=e[i].to;
            if (d[v]!=0||e[i].z<=0) continue;
            d[v]=d[q[head]]+1;
            q[++tail]=v;
        }
    }
    if (d[n+n+1]>0) return true;
    else return false;
}
int dinic(int u,int mn,int fa)
{
    if (u==n+n+1||mn==0) return mn;
    int sum=0,flow=0;
    for (int i=cur[u];i;i=e[i].nxt)
    {
        cur[u]=i;//本代码加入了当前弧优化
        int v=e[i].to;
        if (v==fa||d[v]!=d[u]+1||e[i].z<=0) continue;
        sum=dinic(v,min(mn,e[i].z),u);
        if (sum<=0) continue;
        flow+=sum;
        mn-=sum;
        e[i].z-=sum;
        e[i^1].z+=sum;
        if (mn<=0) break;
    }
    return flow;
}
bool opt()
{
    int res=0;
    while (bfs(0)==true)
        res+=dinic(0,2147483647,0);
    if (res==n) return true;
    else return false;
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
            scanf("%d",&a[i][j]);
    for (int i=1;i<=m;i++)
        scanf("%d",&b[i]);
    int ans=2147483647;
    for (int i=1;i<=m;i++)
    {
        int l=0,r=m-i;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            build(i,i+mid);
            if (opt()==true) ans=min(ans,mid+1),r=mid-1;
            else l=mid+1;
        }   
    }   
    printf("%d\n",ans);
} 

Guess you like

Origin www.cnblogs.com/nibabadeboke/p/12114842.html