zoj 3471

状态压缩 dp

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 105
#define INF 0x3f3f3f3f
#define inf 10000000
#define MOD 100000000
#define ULL unsigned long long
#define LL long long
#define _setm(A) memset(A, INF, sizeof(A))

using namespace std;

int n, g[15][15], dp[1<<15];

int main() {
    // freopen("in.txt", "r", stdin);
    while(scanf("%d", &n) == 1 && n) {
        memset(dp, 0, sizeof(dp));
        for(int i = 0; i < n; ++ i) {
            for(int j = 0; j < n; ++ j) {
                scanf("%d", &g[i][j]);
            }
        }

        for(int i = (1<<n)-1; i >= 0; -- i) {
            for(int j = 0; j < n; ++ j) {
                if((i&(1<<j)) == 0) continue;
                for(int k = 0; k < n; ++ k) {
                    if(j == k || (i&(1<<k)) ==  0) continue;
                    int nS = i-(1<<k);
                    dp[nS] = max(dp[nS], dp[i]+g[j][k]);
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < (1<<n); ++ i) {
            ans = max(ans, dp[i]);
        }
        printf("%d\n", ans);
    }
    return 0;
}

  

posted @ 2014-07-24 10:33  xlc2845  阅读(118)  评论(0编辑  收藏  举报