[Bullpen] JZOJ1259 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.


analysis

  • Half \ (+ \) network flow, the examination did not understand the question less about a situation

  • For each cow, half of it to find the next least the first few bullpen can make all the cattle live under

  • This is determined by matching a network flow or run bipartite graph, so as cow few lines are also enumerated

  • \ (S \) and bovine even flow \ (1 \) side, cows and cow even flow \ (1 \) side, and cow \ (T \) connected to the edge flow capacity cowshed

  • With \ (SAP \) fast, like a record minimum


code

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 2005
#define MAXM 50005
#define INF 1000000007
#define ll long long
#define fo(i,a,b) for (ll i=a;i<=b;++i)
#define fd(i,a,b) for (ll i=a;i<=b;--i)
#define rep(i,a) for (ll i=last[a];i;i=next[i])

using namespace std;

ll last[MAXM],next[MAXM],tov[MAXM],len[MAXM],cur[MAXM];
ll f[21],d[MAXN],gap[MAXN],a[MAXN][21];
ll n,m,S,T,tot=1,ans=INF;

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
    while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*f;
}
inline ll min(ll x,ll y)
{
    return x<y?x:y;
}
inline void link(ll x,ll y,ll z)
{
    next[++tot]=last[x],last[x]=tot,tov[tot]=y,len[tot]=z;
    next[++tot]=last[y],last[y]=tot,tov[tot]=x,len[tot]=0;
}
inline ll dfs(ll x,ll flow)
{
    if (x==T)return flow;
    ll ans=0;
    for (ll i=cur[x];i;i=next[i])
    {
        cur[x]=i;
        if (len[i] && d[tov[i]]+1==d[x])
        {
            ll tmp=dfs(tov[i],min(len[i],flow-ans));
            ans+=tmp,len[i]-=tmp,len[i^1]+=tmp;
            if (ans==flow)return ans;
        }
    }
    cur[x]=last[x];
    if (!(--gap[d[x]]))d[S]=n;
    ++gap[++d[x]];
    return ans;
}
inline ll SAP()
{
    ll flow=0;
    while (d[S]<n)flow+=dfs(S,INF);
    return flow;
}
inline bool judge(ll x,ll y)
{
    memset(last,0,sizeof(last)),memset(next,0,sizeof(next));
    memset(tov,0,sizeof(tov)),memset(len,0,sizeof(len));
    memset(cur,0,sizeof(cur)),tot=1;
    memset(gap,0,sizeof(gap)),memset(d,0,sizeof(d));
    fo(i,1,n)link(S,i,1);fo(i,1,m)link(n+i,T,f[i]);
    fo(i,1,n)fo(j,x,y)link(i,a[i][j]+n,1);
    ll tmp=SAP();
    return tmp==n;
}
inline ll binarysearch(ll x,ll l,ll r)
{
    ll mid=(l+r)>>1;
    while (l<=r)
    {
        mid=(l+r)>>1;
        if (judge(x,mid))r=mid-1;
        else l=mid+1;
    }
    return l;
}
int main()
{
    freopen("T3.in","r",stdin);
    n=read(),m=read();
    fo(i,1,n)fo(j,1,m)a[i][j]=read();
    fo(i,1,m)f[i]=read();
    S=n+m+1,T=S+1;
    fo(k,1,m)
    {
        ll next=binarysearch(k,k,m);
        if (next<=m)ans=min(ans,next-k+1);
    }
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/horizonwd/p/11131689.html