1. 程式人生 > >[ZOJ4257] [ZOJMonthly1102] Most Powerful [狀態壓縮][dp]

[ZOJ4257] [ZOJMonthly1102] Most Powerful [狀態壓縮][dp]

狀壓水題。

#Code
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<queue>
#include<ctime>
using namespace std;
int N,A[12]={};
int Collision[12][12]={};
int F[1200]={};
int main()
{
	while(
~scanf("%d",&N)) { if(!N)return 0; memset(F,0,sizeof(F)); for(int i=1;i<=N;++i) { for(int j=1;j<=N;++j) { scanf("%d",&Collision[i][j]); } } for(int i=0;i<(1<<N);++i) { for(int j=1;j<=N;++j) { if(!(i&(1<<j-1)))continue; for(int
v,k=1;k<=N;++k) { if(j==k)continue; if((i&(1<<k-1)))continue; v=i|(1<<k-1); F[v]=max(F[v],F[i]+Collision[j][k]); } } } printf("%d\n",F[(1<<N)-1]); } return 0; }