zoj 3471 Most Powerful //状态压缩DP



#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = (1 << 10);
int map[11][11];
int dp[N];
int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        if(n == 0)  break;
        for(int i = 0; i < n; i++)
           for(int j = 0; j < n; j++)
           scanf("%d", &map[i][j]);

        memset(dp, 0, sizeof(dp));

        int te = (1 << n) ;
        for(int s = 0; s < te; s++)
        {
            for(int i = 0; i < n; i++)
            {
                if(s & (1 << i))  continue;
                for(int j = 0; j < n; j++)
                {
                    if(s & (1 << j))  continue;
                    if(i == j)  continue;
                    dp[s | (1 << j)] = max(dp[s | (1 << j)], dp[s] + map[i][j]);
                }
            }
        }

        int ans = 0;
        for(int s = 0; s < te; s++)  ans = max(ans, dp[s]);
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(zoj 3471 Most Powerful //状态压缩DP)