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)的更多相关文章

  1. ZOJ - 3471 Most Powerful (状态压缩)

    题目大意:有n种原子,两种原子相碰撞的话就会产生能量,当中的一种原子会消失. 问这n种原子能产生的能量最大是多少 解题思路:用0表示该原子还没消失.1表示该原子已经消失.那么就能够得到状态转移方程了 ...

  2. ZOJ 3471 【状态压缩DP】

    题意: 有n种化学物质,他们彼此反应会有一种消失并释放出能量. 给出矩阵,第i行j列代表i和j反应j消失释放的能量. 求最大释放多少能量. 思路: 状态压缩DP,我是这么想的. 利用二进制0代表该物质 ...

  3. ZOJ 2563 Long Dominoes(状态压缩DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1563 题目大意:在h*w的矩阵里铺满1*3的小矩阵,共有多少种方法 ...

  4. zoj 3471 Most Powerful

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

  5. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  6. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  7. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  8. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  9. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  10. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

随机推荐

  1. RedHat安装GCC问题-解决依赖问题

    RedHat Linux在安装gcc时需要cpp和cloog-ppl但是在安装cpp的时候需要这个依赖:libmpfr.so.1()(64bit) is needed by cpp-4.4.6-3.e ...

  2. spring MVC上传文件演示

    //相比smartUpload功能上感觉确实有点心有意力不足的感觉,就安全性判断后缀,smartUpload就非常方便. public ModelAndView addFileUp(HttpServl ...

  3. JUnit4中的测试套件

    测试套件 JUnit3.8中,用测试套件同时运行多个测试类(http://www.cnblogs.com/mengdd/archive/2013/04/07/3006265.html). 在JUnit ...

  4. Android 环境配置:git开启多颜色模式

    git config --global color.status autogit config --global color.diff autogit config --global color.br ...

  5. 针对各主流数据mysql、sqlserver、oracle中文乱码问题。

    针对各主流数据mysql.sqlserver.oracle当以编码格式gbk存放数据时,要注意字符串类型的字段,要采用宽字符串nvarchar存放,前提是当你的应用程序是utf8编码,而数据库是gbk ...

  6. POJ-1118(超时,但未找到原因)

    #include<iostream> #include<map> #include<vector> using namespace std; //y=kx+z ty ...

  7. [HeadFirst-JSPServlet学习笔记][第二章:高层概述]

    第二章:高层体系结构 容器 1 什么是容器? servelet没有main()方法.它们受控于另一个Java应用,这个Java应用称为容器(Container) Tomcat就是这样一个容器.Web服 ...

  8. js静态方法

    1.ajax() 方法是属于“函数”本身的,和返回的对象没有关系 2.bark药调用,必须药new Hashiqi()得到对象,且由返回对象才能调用 3.ajax()方法药调用,不需要new对象,直接 ...

  9. FOR UPDATE SKIP LOCKED

    SYS_UNIT 中有UNIT_ID 0000000001 0000000002 0000001100 原意为若启用多线程,则每个线程在获取时仅可以获取一条数据(互斥) 脚本如下 1.SELECT * ...

  10. C# 广播TS流精确计时发送

    广播传输相关的项目,需求是UDP发送TS到IP/ASI网关,网关经过ASI输出到激励器,再由激励器通过射频天线输出,接收端为终端机顶盒. 因为以前没有怎么接触过广播相关的东西,一开始认为用C#写个UD ...