POJ3189_Steady Cow Assignment(二分图多重匹配/网络流)

解题报告

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

题目传送门

题意:

B个猪圈,N头猪,每头猪对每个猪圈有一个满意值,要求安排这些猪使得最大满意和最小满意的猪差值最小

思路:

二分图的多重匹配问题;

猪圈和源点连边,容量为猪圈容量,猪与汇点连边,容量1;

猪圈和猪之间连线取决所取的满意值范围;

二分查找满意值最小差值的范围。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define inf 99999999
using 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: 1000MS   Memory Limit: 65536K
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 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.

POJ3189_Steady Cow Assignment(二分图多重匹配/网络流)

时间: 2024-10-10 20:05:17

POJ3189_Steady Cow Assignment(二分图多重匹配/网络流)的相关文章

POJ3189_Steady Cow Assignment(二分图多重匹配/网络流+二分构图)

解题报告 http://blog.csdn.net/juncoder/article/details/38340447 题目传送门 题意: B个猪圈,N头猪.每头猪对每一个猪圈有一个惬意值.要求安排这些猪使得最大惬意和最小惬意的猪差值最小 思路: 二分图的多重匹配问题; 猪圈和源点连边,容量为猪圈容量.猪与汇点连边,容量1; 猪圈和猪之间连线取决所取的惬意值范围; 二分查找惬意值最小差值的范围. #include <iostream> #include <cstring> #inc

POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分

题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6979   Accepted: 2418 Description Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns wh

hihoCoder 1393 网络流三&#183;二分图多重匹配 (网络流学习#3 记录)

题目链接:http://hihocoder.com/problemset/problem/1393 话说我之前一直不知道二分匹配可以用网络流做... #include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=205; struct ss{ int v,c,nxt; } e[N*20]; int head[N],tot,vis[N],n,m,a[N],b[N],s

Optimal Milking(二分图多重匹配+二分)(网络流)

Optimal Milking Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2112 Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 20

hihoCoder 1393 网络流三&#183;二分图多重匹配(Dinic求二分图最大多重匹配)

#1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小Ho作为班上的班干部,统计分配比赛选手的重任也自然交到了他们手上. 已知小Hi和小Ho所在的班级一共有N名学生(包含小Hi和小Ho),编号依次为1..N. 运动会一共有M项不同的比赛,编号为1..M.第i项比赛每个班需要派出m[i]名选手参加. 根据小Hi和小Ho的统计,编号为i的学生表示最多同时参加

hiho 第117周 二分图多重匹配,网络流解决

描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小Ho作为班上的班干部,统计分配比赛选手的重任也自然交到了他们手上. 已知小Hi和小Ho所在的班级一共有N名学生(包含小Hi和小Ho),编号依次为1..N. 运动会一共有M项不同的比赛,编号为1..M.第i项比赛每个班需要派出m[i]名选手参加. 根据小Hi和小Ho的统计,编号为i的学生表示最多同时参加a[i]项比赛,并且给出他所擅长的b[i]项比赛的编号. 小Hi和小Ho希望将每个学生都安排到他所擅长的比赛项目,

网络流24题 第五题 - PowerOJ1740 CodeVS1905 圆桌问题 二分图多重匹配 网络最大流

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - PowerOJ1740 - 有SPJ - 推荐 题目传送门 - CodeVS1905 - 无SPJ - 0% 通过率(可以用来看题目) 题意概括 有n支队伍,m个组.第i支队伍有a[i]个人,第i个组最多可以有b[i]个人. 现在要求任何两个同队队员不可位于同一组,求是否有方案满足. 输出第一行,表示是否有,如果有,是1,没有的话,输出0: 如果有,接下来n行,第i行a[i]个数,表示第i支队伍的每个人被安

Jamie&#39;s Contact Groups(二分图多重匹配+二分)(网络流)

Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2289 Description Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list

poj 3614 Sunscreen 网络流或二分图多重匹配或优先队列

题意: 有C头牛,每头牛有它可以接受的药的最小值和最大值,有L瓶药,每瓶药有一个值u和它最多能给v头牛用,求最多有多少头牛能满足. 分析: 网络流或二分图多重匹配或优先队列,这道题优化了我的dinic模板,原来的模板会TLE... 代码: //poj 3614 //sep9 #include <iostream> #include <queue> #include <algorithm> using namespace std; const int maxN=5005;