ZOJ 3471(状态压缩DP)

题意:有n个原子,每当两个原子碰撞时就会产生能量,并且消耗其中一个原子。已知每两个原子碰撞时消耗其中指定一个原子所产生的能量,问最多能产生多少能量?

状态表示 dp[state] 状态为state时的最大能量

转移方程  dp[state] = max(dp[state],dp[state‘]+a[i][j])

边界条件  dp[i] = 0

#include <algorithm>
#include <cstdio>
#include <cstring>

#define Max 1000001
#define MAXN 11
#define MOD 100000000
#define rin freopen("in.txt","r",stdin)
#define rout freopen("1.out","w",stdout)
#define Del(a,b) memset(a,b,sizeof(a))
#define INF 0x1f1f1f1f
using namespace std;
typedef long long LL;
const int MAXNUM=(1<<MAXN);
int n;
int a[MAXN][MAXN];
int dp[MAXNUM];
int main() {
    //rin;
    while(scanf("%d",&n)!=EOF){
        if(n==0)
            break;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&a[i][j]);
            }
        }
        Del(dp,0);
        int full=1<<n;
        for(int s=0;s<full;s++){
            for(int i=0;i<n;i++){
                if((s&(1<<i)))continue;
                for(int j=0;j<n;j++){
                    if(i==j)continue;
                    if((s&(1<<j)))continue;
                    int newS=(s|(1<<j));//从s状态中删去有气体j的状态
                    dp[newS]=max(dp[newS],dp[s]+a[i+1][j+1]);
                }
            }
        }
        int ans=0;
        for(int s=0;s<full;s++)
            ans=max(ans,dp[s]);
        printf("%d\n",ans);
    }
    return 0;
}

根据s由大到小还是由小到大,转移方程不同。

  for(int i=(1<<n)-1;i>=0;i--)
     for(int j=0;j<n;j++)
     {
         if(i&(1<<j))
         {
           for(int k=0;k<n;k++)
           {
              if(k==j)continue;
              if(i&(1<<k))continue;
              dp[i]=max(dp[i],dp[i|(1<<k)]+a[j][k]);
            }
          }
     } 
时间: 2024-10-09 03:48:11

ZOJ 3471(状态压缩DP)的相关文章

zoj 3471(状态压缩DP,类似于点集配对)

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状态压缩DP

#include<stdio.h> #include<string.h> int max(int a,int b) { if(a>b) return a; return b; } int dp[100000],map[15][15],mark[15]; int main() { int i,j,n,m,k; while(scanf("%d",&n)!=EOF&&n) { for(i=1;i<=n;i++) for(j=1;j&

ZOJ3471 状态压缩DP

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3471 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

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

HDU1565(状态压缩dp)

方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8170    Accepted Submission(s): 3095 Problem Description 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数

HDU 3001【状态压缩DP】

题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数据的预处理,利用数组分解各个位数,从而达到类似二进制的目的. 然后就是状态的表示,dp[s][i]表示状态s时到达i的最优值. 状态转移也一目了然,不废话. #include<stdio.h> #include<string.h> #include<algorithm> u

Victor and World(spfa+状态压缩dp)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 958    Accepted Submission(s): 431 Problem Description After trying hard fo

poj 3311 Hie with the Pie(状态压缩dp)

Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be

HDU--1074(状态压缩DP)

典型的状态压缩DP,给出了每件作业的截止时间和花费,求让老师扣分最少的写作业方式.把完成n种作业用状态2^n-1表示,dp[s]表示 完成状态s时,最小扣分.比如“111”,那么可以由“011”,“110”,“101”转移过来,分别表示选了0,1号作业,1,2号作业,0,2号作业. t[s]表示状态S记录的总时间.dp[s] = min{dp[j]+c[k] - d[k]},其中j = i^(1<<k),0<k<n;pre[s]表示状态s完成时,最末尾完成的作业, #include

poj 3254 Corn Fields(状态压缩dp)

Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and