Reprinted from: http://blog.csdn.net/xindoo/article/details/9173949
Violent tle
1#include <iostream>2#include <string.h>3#include <algorithm>4 5 using namespacestd;6 7 intvis[ -];8 intN, ans, sum;9 intmap[ +][ +];Ten One voidDfsintx) A { - if(x = = n+1) - { theAns =max (ans, sum); - return; - } - for(inti =1; I <= N; i++) + { - if(Vis[i] = =0) + { ASum + =Map[x][i]; atVis[i] =1; -DFS (x+1); -Vis[i] =0; -Sum-=Map[x][i]; - } - } in } - intMain () to { + intT; -CIN >>T; the for(intKase =1; Kase <= t; kase++) * { $CIN >>N;Panax Notoginseng for(inti =1; I <= N; i++) - for(intj =1; J <= N; J + +) theCIN >>Map[i][j]; +sum =0; AAns =0; thememset (Vis,0,sizeof(Vis)); +Dfs1); -cout <<" Case"<< Kase <<": "<< ans <<Endl; $ } $ return 0; -}
State compression +DP
//2013-06-25-22.05#include <iostream>#include<string.h>#include<algorithm>#include<stdio.h>using namespacestd;Const intMAXN =1<< -;intvis[ -];intN, ans, sum;intmap[ +][ +];intDP[MAXN];intDfsintXintd) { if(x = =0) return 0; if(Dp[x])returnDp[x]; for(inti =0; I < n; i++) { if(x& (1<<i)) dp[x]= Max (Dfs (x^ (1<<i), D-1) +map[i+1][d], dp[x]); } returndp[x];}intMain () {intT; CIN>>T; for(intKase =1; Kase <= t; kase++) {cin>>N; for(inti =1; I <= N; i++) for(intj =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;}
Lightoj 1011 Marriage Ceremonies (violent) (memory DP, pressure) turn