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. Python Cookie HTTP获取cookie并处理

    Cookie模块同样是Python标准库中的一员,它定义了一些类来解析和创建HTTP 的 cookie头部信息. 一.创建和设置Cookie >>> import Cookie #导 ...

  2. spring mvc 安全

    1,使用 spring form 标签 防 csrf 攻击 2,标明请求方法:RequestMethod.GET,RequestMethod.POST, PATCH, POST, PUT, and D ...

  3. python高级编程之选择好名称:完2

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #分解代码 #小就是美,这也适用所有级别的代码,当一个函数,类或者一 ...

  4. POJ 3114 Countries in War(强连通+最短路)

    POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...

  5. hadoop多文件输出

    现实环境中,经常遇到一个问题就是想使用多个Reduce,可是迫于setup和cleanup在每个Reduce中会调用一次,仅仅能设置一个Reduce,无法是实现负载均衡. 问题,假设要在reduce中 ...

  6. 【MySql】Linux下更改转移mysql数据库目录

    1.关闭MySql: #service mysqld stop 2.转移数据: #cd /var/lib   #ls   #cp -a mysql /opt/mysql/ 3.修改配置文件,一下三个: ...

  7. jQuery对象和dom对象之间的相互转化

    var domObj = document.getElementById("demo"); var $Obj = $("#demo"); DOM转jQuery: ...

  8. BOM 窗体相关属性以及页面可见区域的获取方式

    1 在IE Safari Oper Chrome 都提供了screenLeft和screenTop属性: screenLeft : 相对于屏幕左边的距离 screenTop : 相对于屏幕上边的距离 ...

  9. ASP.NET实现IE下禁用浏览器后退按钮办法

    在page_load方法里面增加如下代码: Response.Buffer = true; Response.ExpiresAbsolute = DateTime.Parse("2010-1 ...

  10. Intel Core i7的整体操作

    Intel Core i7的整体操作(我们也称呼为Nehalem,他的项目代码名) 主要分成2个部分-指令控制单元Instruction Control Unit(ICU),负责从存储器读出指令序列, ...