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 ...
随机推荐
- C语言漫谈(二) 图像显示 Windows和Linux
关于图像显示有很多库可以用,Windows下有GDI,GDI+,D3D等,Linux下有X Window和Wayland,此外还有OpenGL ,SDL等图形库以及各种GUI库. 了解最原始的方式,对 ...
- ArcEngine颜色可视化
AE中利用.NET中的ColorDialog对话框,将color对象转化为ArcEngine中的IRgbColor (1)在实现颜色选择之前,需定义这两种颜色之间的转换函数 //Color转换为Rgb ...
- PHP学习笔记三十【final】
<?php //final不能去修饰属性(变量) //如果希望类不希望被继承就可以使用final关键字 final class Person() { public function sayHi( ...
- ORA-00937:不是单组分组函数_Oracle
Demo: SELECT USER_ID, USER_NAME, USER_SEX, MAX(USER_AGE), SUM(USER_MONEY) AS USER_MONEY USER_TEL, US ...
- phpMyAdmin批量修改Mysql数据表前缀的方法
多个网站共用一个Mysql数据库时,为使数据库管理不混乱,一般采用不同的网站使用不同前缀名的方式进行区分.而如何批量修改已有数据库的前缀名 呢?全部导出修改后再导入?还是一个表一个表的修改?今天我要介 ...
- WAMP下定义wordpress固定连接出现文章页面404
在修改为如下设置后,一切更新正常,但回到博客主页访问单独文章时出现了404问题 最后一项改为index.php/%postname%后可正常访问,但是去掉那个php文件则不行. 找到的解决方法为:开启 ...
- 学了一个封装的jquery插件,感觉还成
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JQuery中阻止事件冒泡的两种方式及其区别
JQuery 提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...
- python笔记之提取网页中的超链接
python笔记之提取网页中的超链接 对于提取网页中的超链接,先把网页内容读取出来,然后用beautifulsoup来解析是比较方便的.但是我发现一个问题,如果直接提取a标签的href,就会包含jav ...
- js中document的用法
document.title //设置文档标题等价于HTML的title标签document.bgColor //设置页面背景色document.fgColor //设置前景色(文本颜色)docume ...