Note: first, the input of this question is incorrect. We need to replace the bits of the upper and lower matrices to generate an example. That is, the matrix above is the employee's score on the Supervisor. The matrix below is the score of the Supervisor on the employee. Question: two matrices are given, namely, the employee's score for supervision and the employee's score for supervision. Of course, what is given in the matrix is not the score, but the order in which it comes in. The first score is 1 and the second one is... Likewise, the lower the score, the more I like this department. Then ask you to find out the most satisfactory solution and output the average non-satisfaction. The average non-satisfaction is. Assume that the person from a to B has a score of 1, however, after a complete match, he is assigned to c. His score for c is 2, so his dissatisfaction is 1, then the average dissatisfaction is the sum of dissatisfaction/(2 * n ). Idea: build an image. For each employe and supervision, the weight of an edge is the sum of their scores. The next step is to obtain a minimum weight match, because the smaller the weight, the more satisfied they are. In the final output of the average dissatisfaction, why do we need-2 * n? If all the employees and supervision match the edge with the smallest value, then the value of this edge is 2, therefore, if the weight of the current matching result is 2 * n, then everyone is in the most satisfactory position, so dissatisfaction is 0. CODE:
#include <set> #include <map> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <string> #include <vector> #include <iomanip> #include <cstring> #include <iostream> #include <algorithm> #define Max 2505 #define FI first #define SE second #define ll long long #define PI acos(-1.0) #define inf 0x3fffffff #define LL(x) ( x << 1 ) #define bug puts("here") #define PII pair<int,int> #define RR(x) ( x << 1 | 1 ) #define mp(a,b) make_pair(a,b) #define mem(a,b) memset(a,b,sizeof(a)) #define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i ) using namespace std; inline void RD(int &ret) { char c; int flag = 1 ; do { c = getchar(); if(c == '-')flag = -1 ; } while(c < '0' || c > '9') ; ret = c - '0'; while((c=getchar()) >= '0' && c <= '9') ret = ret * 10 + ( c - '0' ); ret *= flag ; } #define N 22 int n ; int Map[N][N] ; int lx[N] , ly[N] , visx[N] , visy[N] ,linkx[N] , linky[N] ; int find(int now){ visx[now] = 1 ; for (int i = 1 ; i <= n ; i ++ ){ if(!visy[i]){ int fk = Map[now][i] - lx[now] - ly[i] ; if(!fk){ visy[i] = 1 ; if(linky[i] == -1 || find(linky[i])){ linky[i] = now ; linkx[now] = i ; return 1 ; } } } } return 0 ; } int KM(){ mem(linky ,-1) ; mem(ly ,0) ; for (int i = 1 ; i <= n ; i ++ ){ lx[i] = inf ; for (int j = 1 ; j <= n ; j ++ )lx[i] = min(lx[i] , Map[i][j]) ; } for (int i = 1 ; i <= n ; i ++ ){ while(1){ mem(visx, 0) ;mem(visy, 0) ; if(find(i))break ; int d = inf ; for (int j = 1 ; j <= n ; j ++ ) if(visx[j]) for (int k = 1 ; k <= n ; k ++ ) if(!visy[k]) d = min(d , Map[j][k] - lx[j] - ly[k]) ; for (int j = 1 ; j <= n ; j ++ ){ if(visx[j])lx[j] += d ; if(visy[j])ly[j] -= d ; } } } int ans = 0 ; for (int i = 1 ; i <= n ; i ++ ){ if(linky[i] != -1)ans += Map[linky[i]][i] ; } return ans ; } int cnt , ans ; bool vis[N] ; int ffk[N] ; void dfs(int dp , int sum){ if(sum > ans)return ; if(dp > n){ if(sum == ans){ printf("Best Pairing %d\n",++ cnt) ; for (int i = 1 ; i <= n ; i ++ ){ printf("Supervisor %d with Employee %d\n",i , ffk[i]) ; } } return ; } else for (int i = 1 ; i <= n ; i ++ ){ if(!vis[i]){ vis[i] = 1 ; ffk[dp] = i ; dfs(dp + 1 , sum + Map[dp][i]) ; vis[i] = 0 ; } } } int main() { int t ; cin >> t ; int ca = 0 ; while(t -- ){ cin >> n ; cnt = 0 ; mem(Map ,0) ; for (int i = 1 ; i <= n ; i ++ ){ for (int j = 1 ; j <= n ; j ++ ){ int fk ; RD(fk) ; Map[fk][i] += j ; } } for (int i = 1 ; i <= n ; i ++ ){ for (int j = 1 ; j <= n ; j ++ ){ int fk ; RD(fk) ; Map[i][fk] += j ; } } mem(vis ,0) ; ans = KM() ; printf("Data Set %d, Best average difference: %f\n",++ ca ,( ans - n * 2 ) * 0.5 / n) ; dfs(1 , 0) ; puts("") ; } return 0 ; }