zoukankan      html  css  js  c++  java
  • 枚举 转化为可行性判定问题 网络流 poj3189

    Steady Cow Assignment
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6914   Accepted: 2387

    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

    二分长度+枚举

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=1500;
    int head[N],tot,S,T;
    int q[N],dis[N],n,m;
    int mp[N][22],cap[22];
    struct node
    {
        int next,v,w;
    } e[N*100];
    void add(int u,int v,int w)
    {
        e[tot].v=v;
        e[tot].w=w;
        e[tot].next=head[u];
        head[u]=tot++;
    }
    bool bfs()
    {
        memset(dis,-1,sizeof(dis));
        dis[S]=0;
        int l=0,r=0;
        q[r++]=S;
        while(l<r)
        {
            int u=q[l++];
            for(int i=head[u]; ~i; i=e[i].next)
            {
                int v=e[i].v;
                if(dis[v]==-1&&e[i].w>0)
                {
                    q[r++]=v;
                    dis[v]=dis[u]+1;
                    if(v==T) return true;
                }
            }
        }
        return false;
    }
    int dfs(int s,int low)
    {
        if(s==T||!low) return low;
        int ans=low,a;
        for(int i=head[s]; ~i; i=e[i].next)
        {
            if(e[i].w>0&&dis[e[i].v]==dis[s]+1&&(a=dfs(e[i].v,min(e[i].w,ans))))
            {
                e[i].w-=a;
                e[i^1].w+=a;
                ans-=a;
                if(!ans) return low;
            }
        }
        if(low==ans) dis[s]=-1;
        return low-ans;
    }
    bool solve(int l,int r)
    {
        memset(head,-1,sizeof(head));
        tot=0;
        int ans=0;
        for(int i=1; i<=n; ++i) add(S,i,1),add(i,S,0);
        for(int i=1; i<=n; ++i) for(int j=l; j<=r; ++j) add(i,mp[i][j]+n,1),add(mp[i][j]+n,i,0);
        for(int i=1; i<=m; ++i) add(i+n,T,cap[i]),add(T,i+n,0);
        while(bfs()) ans+=dfs(S,1008611);
        if(ans==n) return true;
        return false;
    }
    int main()
    {
        scanf("%d%d",&n,&m);S=0,T=n+m+1;
        for(int i=1; i<=n; ++i) for(int j=1; j<=m; ++j) scanf("%d",&mp[i][j]);
        for(int i=1; i<=m; ++i) scanf("%d",&cap[i]);
        int l=1,r=m,ans=m;
        while(l<=r) {
            int mid=(l+r)>>1;
            bool ok=0;
            for(int i=1;i+mid-1<=m;++i) if(solve(i,i+mid-1)) {
                ok=1;
                r=mid-1;
                ans=mid;
            }
            if(!ok) l=mid+1;
        }
        printf("%d ",ans);
    }
  • 相关阅读:
    屏幕取色器colorspy
    js监控按键
    修改硬件信息小工具
    C#SendKeys的用法
    错误分析:程序集未标记为可序列化
    C#xml反序列化
    office2010激活工具
    截图软件
    C#画图
    .net中SQL防注入代码
  • 原文地址:https://www.cnblogs.com/mfys/p/7483265.html
Copyright © 2011-2022 走看看