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 atoms perform when collided and the power every two atoms can produce. You are to write a program to make it most powerful, which means that the sum of power produced during all the collides is maximal.
Input
There are multiple cases. The first line of each case has an integer N ( <= N <= ), which means there are N atoms: A1, A2, ... , AN. Then N lines follow. There are N integers in each line. The j-th integer on the i-th line is the power produced when Ai and Aj collide with Aj gone. All integers are positive and not larger than . The last case is followed by a in one line. There will be no more than cases including no more than large cases that N is .
Output
Output the maximal power these N atoms can produce in a line for each case.
Sample Input
Sample Output
枚举所有的状态,0表示存在,1表示不存在(已经与别的气球产生能量并且消失)。然后就是枚举所有的状态(第一个for循环),再在里面两重循环i和j,i表示不消失的那个,j表示消失的那个气球,得到了一个新的状态newS=S+(1<<j),最后就是dp[newS]=max(dp[newS],dp[S]+mp[i][j]); 边界条件是dp[S]=0, 注意这些条件(1、if(S&(1<<i)) continue; 2、if(i==j)continue; 3、if(S&(1<<j)) continue;)
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 16
#define M 1<<N
#define inf 1e12
int n;
int mp[N][N];
int dp[M];
int main()
{
while(scanf("%d",&n)==){
if(n==) break;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
scanf("%d",&mp[i][j]);
}
} memset(dp,,sizeof(dp));
for(int S=;S<(<<n);S++){
for(int i=;i<n;i++){
if(S&(<<i)) continue;
for(int j=;j<n;j++){
if(i==j) continue;
if(S&(<<j)) continue;
int newS=S+(<<j);
dp[newS]=max(dp[newS],dp[S]+mp[i][j]);
}
}
}
int ans=;
for(int i=;i<(<<n);i++){
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
return ;
}
zoj 3471 Most Powerful(状态压缩dp)的更多相关文章
- ZOJ - 3471 Most Powerful (状态压缩)
题目大意:有n种原子,两种原子相碰撞的话就会产生能量,当中的一种原子会消失. 问这n种原子能产生的能量最大是多少 解题思路:用0表示该原子还没消失.1表示该原子已经消失.那么就能够得到状态转移方程了 ...
- ZOJ 3471 【状态压缩DP】
题意: 有n种化学物质,他们彼此反应会有一种消失并释放出能量. 给出矩阵,第i行j列代表i和j反应j消失释放的能量. 求最大释放多少能量. 思路: 状态压缩DP,我是这么想的. 利用二进制0代表该物质 ...
- ZOJ 2563 Long Dominoes(状态压缩DP)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1563 题目大意:在h*w的矩阵里铺满1*3的小矩阵,共有多少种方法 ...
- zoj 3471 Most Powerful
题目链接:zoj 3471 Most Powerful 作者:jostree 转载请说明出处 很经典的状态dp,使用i的二进制位表示粒子的状态,0表示存在,1表示不存在.dp[i]表示在状态i的情况 ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
随机推荐
- DBA 经典面试题(5)
国外公司的Oracle DBA试题 Oracle DBA Interview Questions 1. How many memory layers are in the shared pool? 2 ...
- View中取设置了Tag的UILabel
UILabel *badge = (UILabel *)[self.view viewWithTag:];
- [Git] --no-verify
Somtimes, the project might set the commit message guide line, if your commit doesn't meet the requi ...
- jquery更新后怎样在一个站点中使用两个版本号的jQuery
公司眼下的项目中的右側导航菜单用到了bootstrap(v3.2.0)的affix.js(Affix插件)与scrospy.js(滚动监听)插件, 须要用到版本号>= 1.9.0的jquery, ...
- Java中的编码格式
Java中的编码 gbk编码 中文占用2个字节,英文占1个字节; utf-8编码 中文占用3个字节.,英文占用1个字节; Java是双字节编码 (utf-16be) utf -16be 中文占2个字节 ...
- jQuery对象和dom对象之间的相互转化
var domObj = document.getElementById("demo"); var $Obj = $("#demo"); DOM转jQuery: ...
- FLASH图片上传功能—从百度编辑器UEditor里面提取出来
为了记录工作中碰到的各种问题,以及学习资料整理,今天开始,将以往的文章进行了一个整理,以后也开始认真的记录学习过程中的各种问题 在HTML里面的文件上传功能一直是个问题,为了实现上传文件大小限制,怎样 ...
- django的Model 模型中常用的字段类型
常用的字段类型: AutoField:自增长字段,通常不用,如果未在Model中显示指定主键,django会默认建立一个整型的自增长主键字段 BooleanField:布尔型,值为True或False ...
- Android拍照与相册选取图片
做过几次拍照,相册选取图片,但都记不住,这次发表个简单的保存下 private static final int PHOTO_GRAPH = 1;// 拍照 private static final ...
- Byte、KB、MB、GB、TB、PB、EB是啥以及它们之间的进率
它们是存储单位 因为计算机存储单位一般用B,KB.MB.GB.TB.PB.EB.ZB.YB.BB来表示,它们之间的关系是: 位 bit (比特)(Binary Digits):存放一位二进制数,即 0 ...