light oj 1011 – Marriage Ceremonies (狀態壓縮+記憶化搜尋)

來源:互聯網
上載者:User

題目連結

     大概題意是有n個男的n個女的(原諒我這麼說,我是粗人),給你一個n*n的矩陣,第i行第j列表示第i個女(男)對第j個男(女)的好感度,然後要安排n對相親,保證都是正常的(無搞基百合之類的),然後求怎麼安排能使好感度和最大,求出最大值。

    開始試了純暴力的方法,時間複雜度是n!果斷逾時

#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;}

後來因為一個小小的弱智的錯誤 wa了5  6次。代碼中用了很多位元運算,通過位元運算很方便的把狀態進行了壓縮儲存起來。

//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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.