E.room(二分图最大权匹配KM算法)

题目描述
Nowcoder University has 4n students and n dormitories ( Four students per dormitory). Students numbered from 1 to 4n.

And in the first year, the i-th dormitory ‘s students are (x1[i],x2[i],x3[i],x4[i]), now in the second year, Students need to decide who to live with.

In the second year, you get n tables such as (y1,y2,y3,y4) denote these four students want to live together.

Now you need to decide which dormitory everyone lives in to minimize the number of students who change dormitory.

输入描述:
The first line has one integer n.

Then there are n lines, each line has four integers (x1,x2,x3,x4) denote these four students live together in the first year

Then there are n lines, each line has four integers (y1,y2,y3,y4) denote these four students want to live together in the second year
输出描述:
Output the least number of students need to change dormitory.
示例1
输入
复制
2
1 2 3 4
5 6 7 8
4 6 7 8
1 2 3 5
输出
复制
2
说明
Just swap 4 and 5
备注:
1<=n<=100

1<=x1,x2,x3,x4,y1,y2,y3,y4<=4n

It’s guaranteed that no student will live in more than one dormitories.

KM算法
第一年的宿舍i和第二年的宿舍j连边,权值为宿舍i变成宿舍j中不同学生的个数
求最小权匹配即得出最少需要换宿舍的学生数

#include 

using namespace std;
/* KM算法
*   复杂度O(nx*nx*ny)
* 求最大权匹配
*   若求最小权匹配,可将权值取相反数,结果取相反数
* 点的编号从0开始
*/

const int N = 110;
const int INF = 0x3f3f3f3f;
int nx, ny; //两边的点数
int g[N][N];//二分图描述
int linker[N], lx[N], ly[N]; //y中各点匹配状态,x,y中的点标号
int slack[N];
bool visx[N], visy[N];

bool DFS(int x) {
    visx[x] = true;
    for(int y = 0; y < ny; y++) {
        if(visy[y])continue;
        int tmp = lx[x] + ly[y] - g[x][y];
        if(tmp == 0) {
            visy[y] = true;
            if(linker[y] == -1 || DFS(linker[y])) {
                linker[y] = x;
                return true;
            }
        } else if(slack[y] > tmp) slack[y] = tmp;
    }
    return false;
}
int KM() {
    memset(linker, -1, sizeof(linker));
    memset(ly, 0, sizeof(ly));
    for(int i = 0; i < nx; i++) {
        lx[i] = -INF;
        for(int j = 0; j < ny; j++)
            if(g[i][j] > lx[i])
                lx[i] = g[i][j];
    }
    for(int x = 0; x < nx; x++)
    {
        for(int i = 0; i < ny; i++) slack[i] = INF;
        while(true) {
            memset(visx, false, sizeof(visx));
            memset(visy, false, sizeof(visy));
            if(DFS(x))break;
            int d = INF;
            for(int i = 0; i < ny; i++)
                if(!visy[i] && d > slack[i]) d = slack[i];
            for(int i = 0; i < nx; i++)
                if(visx[i])
                    lx[i] -= d;
            for(int i = 0; i < ny; i++) {
                if(visy[i])ly[i] += d;
                else slack[i] -= d;
            }
        }
    }
    int res = 0;
    for(int i = 0; i < ny; i++)
        if(linker[i] != -1)
            res += g[linker[i]][i];
    return res;
}

int main() {
    int n;
    while(scanf("%d", &n) == 1) {
        int x[110][4];
        int y[110][4];
        for(int i=0;ifor(int j=0;j<4;j++){
                scanf("%d",&x[i][j]);
            }
        }
        for(int i=0;ifor(int j=0;j<4;j++){
                scanf("%d",&y[i][j]);
            }
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                int cnt=0;
                for(int k=0;k<4;k++){
                    for(int l=0;l<4;l++){
                        if(x[i][k]==y[j][l]){
                            cnt++;
                            break;
                        }
                    }
                }
                g[i][j]=cnt-4;
            }
        }
        nx = ny = n;
        printf("%d\n", -KM());
    }
    return 0;
}

你可能感兴趣的:(牛客网暑假训练营,二分图)