Most Powerful(ZOJ 3471状压dp)

题意:n个原子,两两相撞其中一个消失,产生能量,给出任意两原子相撞能产生的能量,求能产生的最大能量。

分析:dp[i]表示情况为i时产生的最大能量

/*#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
int n,dp[2000],a[15][15];
void solve(){
    memset(dp,0,sizeof(dp));
    for(int i=(1<<n)-1;i>0;--i){
            int maxv=0,m=i;
        for(int j=0;j<n;++j){
            if((m&1)==0){
                int maxp=0,t=i;
                for(int k=0;k<n;++k){
                    if((t&1)&&a[k][j]>maxp)
                        maxp=a[k][j];
                    t>>=1;
                }
            if(dp[i^(1<<j)]+maxp>maxv)
                maxv=dp[i^(1<<j)]+maxp;
            }
            m>>=1;
        }
        dp[i]=maxv;
    }
    int maxpow=-1;
    for(int i=0;i<n;++i){
        if(maxpow<dp[(1<<i)])
        maxpow=dp[(1<<i)];
        //cout<<dp[(1<<i)]<<endl;
    }
    printf("%d\n",maxpow);
}
int main()
{
    while(~scanf("%d",&n)){
        if(n==0)break;
        for(int i=0;i<n;++i)
            for(int j=0;j<n;++j)
            scanf("%d",&a[i][j]);
        solve();
    }
return 0;
}*/
//
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
int dp[1100],n;
int power[15][15];
void solve(){
    memset(dp,0,sizeof(dp));
    int cas=(1<<n);
    for(int i=0;i<cas;++i)
    for(int j=0;j<n;++j){
        if((i&(1<<j)))continue;
        int maxv=-1;
        for(int k=0;k<n;++k)
            if((i&(1<<k))==0&&j!=k)
        dp[i|(1<<j)]=max(dp[i|(1<<j)],dp[i]+power[k][j]);
    }
    int maxv=-1;
    for(int i=0;i<cas;++i)
        maxv=max(maxv,dp[i]);
    printf("%d\n",maxv);
}
int main()
{
    while(~scanf("%d",&n)){
        if(n==0)break;
        for(int i=0;i<n;++i)
            for(int j=0;j<n;++j)
            scanf("%d",&power[i][j]);
        solve();
    }
return 0;
}

  

时间: 2024-12-11 23:01:21

Most Powerful(ZOJ 3471状压dp)的相关文章

zoj 3471 状压DP

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 难度远不及我之前发的... 可是我第一次的思路居然错了,由于dp方程想设计成二维,可是弄错.也没发现原因.,. 改为一维:dp[s]:状态为s的时候,得到的最大能量,当中s第i位为1表示,i已经被撞毁 #include <cstdio> #include <cstring> #include <algorithm> #include <s

ZOJ 3306 状压dp

转自:http://blog.csdn.net/a497406594/article/details/38442893 Kill the Monsters Time Limit: 7 Seconds Memory Limit: 32768 KB In order to celebrate the 8th anniversary of ZOJ, watashi introduces a strange game to other ZJU ACM team members. The board of

zoj 3675 状压dp

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918 昨天的排位,最初我还以为思维题,然后队友说状压DP,直接放弃,赛后看了队友的代码,在搜下网上的,发现队友的代码居然是最短的,膜拜啊~~~~~~~ 思路是队友 A.L.的 dp[s]=min(dp[s],dp[s']+1) 其中s'可以由s通过一次正着剪指甲或者反着剪指甲达到 至于内层循环,0-m,是因为----从剪指甲刀的最后一位看,最后一位从0移动到m,足够所有情况了

Survival(ZOJ 2297状压dp)

题意:有n个怪,已知杀死第i个怪耗费的血和杀死怪恢复的血,和杀死boss耗的血,血量不能超过100,若过程中血小于0,则失败,问 是否能杀死boss(boss最后出现). 分析:就是求杀死n个怪后剩余的最大血量 dp[i]表示杀怪情况为i(0未杀,1已杀)时剩余最大血量,注意血量上限为100,过程中血小于0,则失败即可. #include <map> #include <set> #include <list> #include <cmath> #inclu

Travel(HDU 4284状压dp)

题意:给n个城市m条路的网图,pp在城市1有一定的钱,想游览这n个城市(包括1),到达一个城市要一定的花费,可以在城市工作赚钱,但前提有工作证(得到有一定的花费),没工作证不能在该城市工作,但可以走,一个城市只能工作一次,问pp是否能游览n个城市回到城市1. 分析:这个题想到杀怪(Survival(ZOJ 2297状压dp) 那个题,也是钱如果小于0就挂了,最后求剩余的最大钱数,先求出最短路和 Hie with the Pie(POJ 3311状压dp) 送披萨那个题相似. #include <

ZOJ 3471 Most Powerful 状压DP

水题,一维的DP,表示还剩哪些atom的时候能获得的最大能量 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include <

状压DP [ZOJ 3471] Most Powerful

Most Powerful Time Limit: 2 Seconds      Memory Limit: 65536 KB Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a

ZOJ 3471 Most Powerful(状压DP)

Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way every two at

ZOJ 3471 Most Powerful(状压DP)

Description Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way