题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5691
题意:中文,不解释
题解:设dp[i][j]表示当前状态为i,以第j个数为末尾的最忧解,然后dp下去就行了
1 #include<cstdio> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 inline void up(int &x,int y){if(x<y)x=y;} 4 5 int t,n,dp[1<<16][17],a[17],b[17],inf=-(1<<30),end,ans,ic=1; 6 7 int main(){ 8 scanf("%d",&t); 9 while(t--){ 10 scanf("%d",&n),end=(1<<n)-1,ans=inf; 11 F(i,1,n)scanf("%d%d",a+i,b+i),b[i]++; 12 F(i,0,end)F(j,1,n)dp[i][j]=inf; 13 F(i,1,n)if(!b[i]||b[i]==1)dp[1<<(i-1)][i]=0; 14 F(i,0,end){ 15 int have=__builtin_popcount(i);//返回该数的二进制1的个数 16 F(j,1,n)if(dp[i][j]>inf) 17 F(k,1,n)if(j==k||(b[k]&&b[k]!=have+1)||i&1<<(k-1))continue; 18 else up(dp[i|1<<(k-1)][k],dp[i][j]+a[j]*a[k]); 19 } 20 F(i,1,n)up(ans,dp[end][i]); 21 printf("Case #%d: %d ",ic++,ans); 22 } 23 return 0; 24 }