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 (2 <= N <= 10), 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 10000.

The last case is followed by a 0 in one line.

There will be no more than 500 cases including no more than 50 large cases that N is 10.

Output

Output the maximal power these N atoms can produce in a line for each case.

Sample Input

2

0 4

1 0

3

0 20 1

12 0 1

1 10 0

0

Sample Output

4

22

题意是有n个气球,n的范围是1~n,每两个气球碰撞都会产生一定的能量,并且有一个气球会消失,问最后剩下一个气球的时候最多能产生多少能量。可以用状态压缩,设0表示气体存在,1表示气体消失,状态转移方程dp[state]=max{dp[state],dp[state']+a[i][j]}.

#include<stdio.h>
#include<string.h>
int a[15][15],dp[1500];
int max(int a,int b){
return a>b?a:b;
}
int main()
{
int n,m,i,j,s,ans;
while(scanf("%d",&n)!=EOF && n!=0)
{
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
memset(dp,0,sizeof(dp));
for(s=1;s<(1<<n);s++){
for(j=1;j<=n;j++){
if(s&(1<<(j-1))){
for(i=1;i<=n;i++){
if( !(s&(1<<(i-1))) ){
dp[s]=max(dp[s],dp[s-(1<<(j-1))]+a[i][j]);
}
}
}
}
}
ans=0;
for(i=1;i<=n;i++){
if(dp[(1<<n)-1-(1<<(i-1))]>ans)ans=dp[(1<<n)-1-(1<<(i-1))];
}
printf("%d\n",ans);
}
return 0;
}

zoj3471 Most Powerful的更多相关文章

  1. ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)

    Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...

  2. 【状压dp】Most Powerful

    [ZOJ3471]Most Powerful Time Limit: 2 Seconds      Memory Limit: 65536 KB Recently, researchers on Ma ...

  3. HDOJ 3593 The most powerful force

    树形DP / 泛化物品的背包...可以去看09年徐持衡论文<浅谈几类背包问题> The most powerful force Time Limit: 16000/8000 MS (Jav ...

  4. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  5. hdu 4150 Powerful Incantation

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4150 Powerful Incantation Description Some dangerous ...

  6. D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

    莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...

  7. 10+ powerful debugging tricks with Visual Studio

    10+ powerful debugging tricks with Visual Studio Original link : http://www.codeproject.com/Articles ...

  8. zoj 3471 Most Powerful

    题目链接:zoj 3471 Most Powerful  作者:jostree 转载请说明出处 很经典的状态dp,使用i的二进制位表示粒子的状态,0表示存在,1表示不存在.dp[i]表示在状态i的情况 ...

  9. zoj 3471 Most Powerful(状态压缩dp)

    Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These ato ...

随机推荐

  1. js的函数-function

    function函数 function的英文是[功能],[数] 函数:职责:盛大的集会的意思 在js里,就是函数的意思.在Java里叫做方法. 定义函数 function fun(参数){ //函数体 ...

  2. Zabbix 4.0.24 完整安装

    依赖包安装: yum install net-snmp* libssh-devel libssh2-devel -y Zabbix server安装: wget https://cdn.zabbix. ...

  3. select 里面带的值居然是估算的?

    mysql> set profiling=1;Query OK, 0 rows affected, 1 warning (0.07 sec) mysql> select count(1) ...

  4. 【Web】HTML入门小结

    文章目录 HTML? HTML 初识元素/标签 HTML语义化标签 标题 段落 font HTMl链接 HTML图像 HTML列表 HTML div HTML 块级元素与行内元素 HTML常用带格式作 ...

  5. 【十天自制软渲染器】DAY 02:画一条直线(DDA 算法 & Bresenham’s 算法)

    推荐关注公众号「卤蛋实验室」或访问博客原文,更新更及时,阅读体验更佳 第一天我们搭建了 C++ 的运行环境并画了一个点,根据 点 → 线 → 面 的顺序,今天我们讲讲如何画一条直线. 本文主要讲解直线 ...

  6. kafka(一)入门

    一.消息引擎系统 这类系统引以为豪的消息传递属性,像引擎一样,具备某种能量转换传输的能力 消息引擎系统是一组规范,企业利用这组规范在不同系统之间传递语义准确的消息,实现松耦合的异步式数据传递.通俗地讲 ...

  7. JDBC代码的优化

    JDBC代码简化以及PreparedStatement和Statement接口 抽取 JDBC的Bug sql语句可以拼接导致登录功能中如果用户名或者密码中出现'or'2'='2则一定可以登录的bug ...

  8. 代码 or 指令,浅析ARM架构下的函数的调用过程

    摘要:linux程序运行的状态以及如何推导调用栈. 1.背景知识 1.ARM64寄存器介绍: 2.STP指令详解(ARMV8手册): 我们先看一下指令格式(64bit),以及指令对于寄存机执行结果的影 ...

  9. 当Vue可视化工具创建不了项目时的解决办法!

    当Vue可视化工具创建不了项目时的解决办法! 当你尝试用可视化工具创建一个Vue的项目的时候,报错, 出现什么indexOf什么什么的错误! 我的解决办法是把可视化工具删除掉,重新下载! 如果你是 n ...

  10. spark开窗函数

    源文件内容示例: http://bigdata.beiwang.cn/laoli http://bigdata.beiwang.cn/laoli http://bigdata.beiwang.cn/h ...