zoukankan      html  css  js  c++  java
  • 【ZOJ4257】Most Powerful

    题目描述

    不超过10种气体,两两之间相互碰撞可以产生一定的能量,如a碰b,那么b气体就消失,自身不能碰自身,问最后所能得到的最大能量。

    样例输入

    2
    0 4
    1 0
    3
    0 20 1
    12 0 1
    1 10 0
    0


    样例输出

    4
    22



    题解

    比较简单的状压dp。

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define ll long long
    
    const int maxn=10+5;
    const int maxm=(1<<10)+50;
    
    int n,dp[maxm],d[maxn][maxn],ans;
    
    template<typename T>void read(T& aa){
        char cc; ll ff;aa=0;cc=getchar();ff=1;
        while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
        if(cc=='-') ff=-1,cc=getchar();
        while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
        aa*=ff;
    }
    
    int main(){
        while(scanf("%d",&n)==1&&n){
            ans=0;
            memset(dp,0,sizeof(dp));
            memset(d,0,sizeof(d));
            for(int i=0;i<n;i++)
            for(int j=0;j<n;j++) read(d[i][j]);
            for(int s=(1<<n)-1;s>0;s--){
                for(int i=0;i<n;i++){
                    if(!(s&(1<<i))) continue;
                    for(int j=0;j<n;j++){
                        if(j==i||!(s&(1<<j))) continue;
                        dp[s^(1<<i)]=max(dp[s^(1<<i)],dp[s]+d[j][i]);
                    }
                }
            }
            for(int i=0;i<n;i++) ans=max(ans,dp[(1<<i)]);
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    P1772 [ZJOI2006]物流运输
    P4290 [HAOI2008]玩具取名
    P1859 不听话的机器人
    P1841 [JSOI2007]重要的城市
    P2182 翻硬币
    P1908 逆序对(归并排序)
    P1010 幂次方(分治)
    P3386 【模板】二分图匹配
    P2158 [SDOI2008]仪仗队
    P1582 倒水(贪心 + lowbit)
  • 原文地址:https://www.cnblogs.com/rlddd/p/9846098.html
Copyright © 2011-2022 走看看