Light OJ 1011-marriage ceremonies (State compression + Memory search)

Source: Internet
Author: User
Tags bitwise

Question Link

Probably there are n male and n female (forgive me for saying so, I am a rough man), and I will give you a matrix of N * n, column J of row I indicates the goodwill of the female (male) to the male (female), and then arranges n pairs of blind dates, ensure that they are all normal (no Lily or similar), and then find out how to arrange to make sense and maximum, and find the maximum value.

I started to try the brute-force method. The time complexity is n! Decisive timeout

#include <iostream>#include <string.h>#include <algorithm>using namespace std;int vis[17];int n, ans, sum;int map[19][19];void dfs(int x){    if (x == n+1)    {        ans = max(ans, sum);        return;    }    for (int i = 1; i <= n; i++)    {        if (vis[i] == 0)        {            sum += map[x][i];            vis[i] = 1;            dfs(x+1);            vis[i] = 0;            sum -= map[x][i];        }    }}int main(){    int t;    cin >> t;    for (int kase = 1; kase <= t; kase++)    {        cin >> n;        for (int i = 1; i <= n; i++)            for (int j = 1; j <= n; j++)                cin >> map[i][j];        sum = 0;        ans = 0;        memset(vis, 0, sizeof(vis));        dfs(1);        cout << "Case " << kase << ": " << ans << endl;    }    return 0;}

Then, because of a small mental retardation error, WA had five or six times. The Code uses many bitwise operations to compress and store the State conveniently through bitwise operations.

//2013-06-25-22.05#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>using namespace std;const int maxn = 1<<16;int vis[17];int n, ans, sum;int map[19][19];int dp[maxn];int dfs(int x, int d){    if (x == 0)        return 0;    if (dp[x])        return dp[x];    for (int i = 0; i < n; i++)    {        if (x&(1<<i))            dp[x] = max(dfs(x^(1<<i), d-1)+map[i+1][d], dp[x]);    }    return dp[x];}int main(){    int t;    cin >> t;    for (int kase = 1; kase <= t; kase++)    {        cin >> n;        for (int i = 1; i <= n; i++)            for (int j = 1; j <= n; j++)                cin >> map[i][j];        memset(dp, 0, sizeof(dp));        ans = dfs((1<<n)-1, n);        printf("Case %d: %d\n", kase, ans);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.