lightoj 1011 - Marriage Ceremonies 【状压dp or KM】

来源:互联网 发布:乔丹生涯数据统计 编辑:程序博客网 时间:2024/05/19 05:40

题目链接:lightoj 1011 - Marriage Ceremonies

1011 - Marriage Ceremonies
PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB
You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job is quite hard for you.

The job gets more difficult when people come here and give their bio-data with their preference about opposite gender. Some give priorities to family background, some give priorities to education, etc.

Now your company is in a danger and you want to save your company from this financial crisis by arranging as much marriages as possible. So, you collect N bio-data of men and N bio-data of women. After analyzing quite a lot you calculated the priority index of each pair of men and women.

Finally you want to arrange N marriage ceremonies, such that the total priority index is maximized. Remember that each man should be paired with a woman and only monogamous families should be formed.

Input
Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains an integer N (1 ≤ n ≤ 16), denoting the number of men or women. Each of the next N lines will contain N integers each. The jth integer in the ith line denotes the priority index between the ith man and jth woman. All the integers will be positive and not greater than 10000.

Output
For each case, print the case number and the maximum possible priority index after all the marriages have been arranged.

Sample Input
Output for Sample Input
2
2
1 5
2 1
3
1 2 3
6 5 4
8 1 2
Case 1: 7
Case 2: 16

题意:权值最大匹配。

dp思路:dp[i][j]处理到第i行状态为j的最优解。
KM就是一个模板,不说了。。。

AC代码:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define CLR(a, b) memset(a, (b), sizeof(a))using namespace std;typedef long long LL;const int MAXN = 1e5 +10;const int MOD = 1e6 + 7;int dp[2][1<<16];int Map[16][16];int main(){    int t, kcase = 1;    scanf("%d", &t);    while(t--) {        int n; scanf("%d", &n);        for(int i = 0; i < n; i++) {            for(int j = 0; j < n; j++) {                scanf("%d", &Map[i][j]);            }        }        CLR(dp, 0); int op = 0;        for(int i = 0; i < n; i++) {            dp[0][1<<i] = Map[0][i];        }        for(int i = 1; i < n; i++) {            op = 1 - op;            for(int j = 0; j < (1<<n); j++) {                if(dp[1-op][j]) {                    for(int k = 0; k < n; k++) {                        if(j & (1<<k)) continue;                        dp[op][j|(1<<k)] = max(dp[op][j|(1<<k)], dp[1-op][j] + Map[i][k]);                    }                }            }        }        printf("Case %d: %d\n", kcase++, dp[op][(1<<n)-1]);    }    return 0;}

KM:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include <stack>#define PI acos(-1.0)#define CLR(a, b) memset(a, (b), sizeof(a))#define fi first#define se second#define ll o<<1#define rr o<<1|1using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 20 + 10;const int pN = 1e6;// <= 10^7const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;void add(LL &x, LL y) { x += y; x %= MOD; }int match[MAXN];int lx[MAXN], ly[MAXN];int slack[MAXN], Map[MAXN][MAXN];bool visx[MAXN], visy[MAXN];int nx, ny;int DFS(int x) {    visx[x] = true;    for(int y = 0; y < ny; y++) {        if(visy[y]) continue;        int t = lx[x] + ly[y] - Map[x][y];        if(t == 0) {            visy[y] = true;            if(match[y] == -1 || DFS(match[y])) {                match[y] = x;                return 1;            }        }        else if(slack[y] > t) {            slack[y] = t;        }    }    return 0;}int KM() {    CLR(match, -1); CLR(ly, 0);    for(int x = 0; x < nx; x++) {        lx[x] = -INF;        for(int y = 0; y < ny; y++) {            if(Map[x][y] > lx[x]) {                lx[x] = Map[x][y];            }        }    }    for(int x = 0; x < nx; x++) {        for(int i = 0; i < ny; i++) {            slack[i] = INF;        }        while(1) {            CLR(visx, 0); CLR(visy, 0);            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(match[i] != -1) {            res += Map[match[i]][i];        }    }    return res;}int main(){    int t, kcase = 1;    scanf("%d", &t);    while(t--) {        int n; scanf("%d", &n);        nx = ny = n;        for(int i = 0; i < n; i++) {            for(int j = 0; j < n; j++) {                scanf("%d", &Map[i][j]);            }        }        printf("Case %d: %d\n", kcase++, KM());    }    return 0;}
0 0
原创粉丝点击