zoukankan      html  css  js  c++  java
  • room 二分图最大匹配KM

    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define INF 0x3f3f3f3f
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    #define pqueue priority_queue
    #define NEW(a,b) memset(a,b,sizeof(a))
    const double pi=4.0*atan(1.0);
    const double e=exp(1.0);
    const int maxn=1e5+8;
    typedef long long LL;
    typedef unsigned long long ULL;
    const LL mod=1e9+7;
    const ULL base=1e7+7;
    using namespace std;
    struct room{
        int s[4];
    }a[110],b[110];
    inline int get(room x,room y){
        int sum=0;
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                if(x.s[i]==y.s[j]){
                    sum++;
                }
            }
        }
        return sum;
    }
    int cx[110],cy[110];
    int ch[110][110];
    bool visx[110],visy[110];
    int lx[110],ly[110];
    int n;
    int inc;
    inline bool dfs(int u){
        visx[u]=1;
        for(int i=0;i<n;i++){
            if(!visy[i]){
                int tmp=lx[u]+ly[i]-ch[u][i];
                if(tmp==0){
                    visy[i]=1;
                    if(cy[i]==-1||dfs(cy[i])){
                        cx[u]=i;
                        cy[i]=u;
                        return 1;
                    }
                }
                else if(tmp>0){
                    inc=min(inc,tmp);
                }
            }
        }
        return 0;
    }
    inline void KM(){
        memset(cy,-1,sizeof(cy));
        memset(cx,-1,sizeof(cx));
        for(int i=0;i<n;i++){
            lx[i]=-INF;
            ly[i]=0;
            for(int j=0;j<n;j++){
                lx[i]=max(lx[i],ch[i][j]);
            }
        }
        for(int u=0;u<n;u++){
            while(1){
                memset(visx,0,sizeof(visx));
                memset(visy,0,sizeof(visy));
                inc=INF;
                if(dfs(u)) break;
                for(int i=0;i<n;i++){
                    if(visx[i]){
                        lx[i]-=inc;
                    }
                }
                for(int i=0;i<n;i++){
                    if(visy[i]){
                        ly[i]+=inc;
                    }
                }
            }
        }
    }
    int main(){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            for(int j=0;j<4;j++){
                scanf("%d",&a[i].s[j]);
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<4;j++){
                scanf("%d",&b[i].s[j]);
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                ch[i][j]=get(a[i],b[j]);
            }
        }
        KM();
        int sum=0;
        for(int i=0;i<n;i++){
            sum+=ch[i][cx[i]];
        }
        sum=4*n-sum;
        printf("%d
    ",sum);
    }
  • 相关阅读:
    YTU 2543: 数字整除
    YTU 2542: 弟弟的作业
    YTU 2541: 汽水瓶
    YTU 2535: C++复数运算符重载(+与<<)
    YTU 2530: 小勇玩lol
    YTU 2520: 小慧唱卡拉OK
    YTU 2517: 打倒魔王↖(^ω^)↗
    YTU 2516: 剪刀石头布
    reload、replace、href、assign、window.history.go(0)的区别
    js 数组排序sort方法
  • 原文地址:https://www.cnblogs.com/Profish/p/9612633.html
Copyright © 2011-2022 走看看