• hdu_5691_Sitting in Line(状压DP)


    题目连接: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 }
    View Code


    
    
    
  • 相关阅读:
    HDU-2502-月之数
    C语言的位运算的优势
    HDU-1026-Ignatius and the Princess I
    HDU-1015-Safecracker
    HDU-1398-Square Coins
    HDU-1028-Ignatius and the Princess III
    背包的硬币问题
    HDU-1527-取石子游戏
    HDU-1996-汉诺塔VI
    css中的选择器
  • 原文地址:https://www.cnblogs.com/bin-gege/p/5696102.html
Copyright © 2020-2023  润新知