• POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】


     Steady Cow Assignment
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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.
     
     
    题目大意:给你N表示有N头牛,B表示有B个牛棚。下边N*B的矩阵A,N行,表示N头牛,B列表示喜爱排名从高到低。那么A(i,j)表示第i头牛喜欢排名为j的是哪个牛棚,最后一行为每个牛棚能装多少头牛。数据保证N头牛一定能都安排到牛棚中。现在让你求最小的排名区间长度。
     
    解题思路:一对多,多重匹配。二分枚举区间长度,同时枚举排名左端点和排名右端点。如果匹配成功,说明这个区间长度是可以的,记录答案,同时向小逼近,如果不成功,向大逼近。
     
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<vector>
    #include<iostream>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int maxn = 1100;
    int Map[maxn][maxn];
    int linker[maxn][maxn], used[maxn], cap[maxn];
    bool dfs(int u,int rn,int st,int en){
        for(int v = 1; v <= rn; v++){
            if(used[v] ){
                continue;
            }
            if(Map[u][v] > en || Map[u][v] < st){
                continue;
            }
            used[v] = 1;
            if(linker[v][0] < cap[v]){
                linker[v][++linker[v][0]] = u;
                return true;
            }else{
                for(int j = 1; j <= linker[v][0]; j++){
                    if(dfs(linker[v][j],rn,st,en)){
                        linker[v][j] = u;
                        return true;
                    }
                }
            }
        }
        return false;
    }
    bool Hungary(int ln,int rn,int mid){
        int en ;
        for(int st = 1; st <= rn -mid + 1; st++){
            en = st + mid - 1;
            int ret = 0;
            for(int i = 0; i <= rn; i++){
                linker[i][0] = 0;
            }
            for(int i = 1; i <= ln; i++){
                memset(used,0,sizeof(used));
                if(dfs(i,rn,st,en)){
                    ret++;
                }
            }
            if(ln == ret){
                return true;
            }
        }
        return false;
    }
    int main(){
        int N, B;
        int matrix[1200][50];
        while(scanf("%d%d",&N,&B)!=EOF){
            int c;
            for(int i = 1; i <= N; i++){
                for(int j = 1; j <= B; j++){
                    scanf("%d",&c);
                    Map[i][c] = j;
                }
            }
            for(int i = 1; i <= B; i++){
                scanf("%d",&cap[i]);
            }
            int l = 1, r = B, ans;
            while(l <= r){
                int mid = (l+r)/2;
                if(Hungary(N,B,mid)){
                    r = mid -1;
                    ans = mid;
                }else{
                    l = mid + 1;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      

    还有一种最开始想到的,每次枚举,每次建图,而不是限制区间,时间没有上面的快,但是更好理解。

    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<vector>
    #include<iostream>
    using namespace std;
    const int INF = 9999999;
    const int maxn = 1100;
    int Map[maxn][maxn];
    int linker[maxn][maxn], used[maxn], cap[maxn];
    bool dfs(int u,int rn){
        for(int v = 1; v <= rn; v++){
            if(used[v] || !Map[u][v]){
                continue;
            }
            used[v] = 1;
            if(linker[v][0] < cap[v]){
                linker[v][++linker[v][0]] = u;
                return true;
            }else{
                for(int j = 1; j <= linker[v][0]; j++){
                    if(dfs(linker[v][j],rn)){
                        linker[v][j] = u;
                        return true;
                    }
                }
            }
        }
        return false;
    }
    bool Hungary(int ln,int rn){
        int ret = 0;
        for(int i = 0; i <= rn; i++){
            linker[i][0] = 0;
        }
        for(int i = 1; i <= ln; i++){
            memset(used,0,sizeof(used));
            if(dfs(i,rn)){
                ret++;
            }
        }
        if(ln == ret){
            return true;
        }
        return false;
    }
    int main(){
        int N, B;
        int matrix[1200][50];
        while(scanf("%d%d",&N,&B)!=EOF){
            for(int i = 1; i <= N; i++){
                for(int j = 1; j <= B; j++){
                    scanf("%d",&matrix[i][j]);
                }
            }
            for(int i = 1; i <= B; i++){
                scanf("%d",&cap[i]);
            }
            int l = 1, r = B, ans;
            while(l <= r){
                int mid = (l+r)/2;
                int flag = 0;
                for(int i = 1; i <= B - mid + 1; i++){
                    memset(Map,0,sizeof(Map));
                    for(int j = 1; j <= N; j++){
                        for(int k = i; k < i + mid; k++){
                            Map[j][matrix[j][k]] = 1;
                        }
                    }
                    if(Hungary(N,B)){
                        flag = 1; break;
                    }
                }
                if(flag){
                    r = mid - 1;
                    ans = mid;
                }else{
                    l = mid + 1;
                }
            }
            printf("%d
    ",ans);
    
        }
        return 0;
    }
    

      

     
  • 相关阅读:
    spring-tool-suite-4-4.3.2.RELEASE-e4.12.0-win32.win32.x86_64 下载
    day39_Spring学习笔记_07_CRM_03
    MyEclipse 中 报错 ERROR PARSER:56
    day38_Spring学习笔记_06_CRM_02
    最简单的递归/死循环
    day37_Spring学习笔记_05_CRM_01
    如何在Linux中发现IP地址冲突
    如何在Linux中用命令行工具管理KVM虚拟环境
    使用 Shell 脚本自动化 Linux 系统维护任务
    Linux系统多网卡绑定实战
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4961319.html
Copyright © 2020-2023  润新知