问题

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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int inf=0x3f3f3f3f;
int pho[][];
int dp[<<];
int main()
{
int n;
scanf("%d",&n);
while(n)
{
int ans=-inf;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&pho[i][j]);
}
}
for(int i=;i<(<<n);i++)
{
dp[i]=-inf;
}
int num;
for(int s=;s<(<<n);s++)
{
num=;
for(int i=;i<=n;i++)
{
if(s&(<<(i-)))
{
if(s==(<<(i-)))
{
for(int j=;j<=n;j++)
{
if(i==j)
continue;
dp[s]=max(dp[s],pho[j][i]);
}
}
else
{
for(int j=;j<=n;j++)
{
if((s&(<<(j-)))==)
{
dp[s]=max(dp[s],dp[s^(<<(i-))]+pho[j][i]);
}
}
}
}
else
{
num++;
}
}
if(num==)
{
ans=max(ans,dp[s]);
}
}
printf("%d\n",ans);
scanf("%d",&n);
}
}

ZOJ3471Most Powerful(状态压缩)的更多相关文章

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

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

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

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

  3. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  4. HDU 3605:Escape(最大流+状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...

  5. [HDU 4336] Card Collector (状态压缩概率dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

  6. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  7. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  8. NOIP2005过河[DP 状态压缩]

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  9. vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  10. hoj2662 状态压缩dp

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

随机推荐

  1. 利用python将excel数据解析成json格式

    利用python将excel数据解析成json格式 转成json方便项目中用post请求推送数据自定义数据,也方便测试: import xlrdimport jsonimport requests d ...

  2. php composer 开发自己的包

    以往都是在项目直接写自己的包文件,并没有把他放在packagist上面,以composer来管理使用. 今天没事来整一下,供大家一起学习 一,在github和packagist分别注册自己的账号,这里 ...

  3. python高级篇

    1.切片功能:类似于java中的split方法.对list或者triple中几个值进行取出的过程. L = ['a','b','c','d']   L[0:3] = ['a','b','c']   # ...

  4. 扩展Caylay定理

    参考资料: https://hyscere.github.io/2019/09/05/%E6%89%A9%E5%B1%95Caylay%E5%AE%9A%E7%90%86/ https://www.c ...

  5. classmethod自己定制

    # # 利用描述符原理定义一个@classmethod # class ClassMethod: # def __init__(self,func): # self.func = func # def ...

  6. 【Linux开发】linux设备驱动归纳总结(五):2.操作硬件——IO内存

    linux设备驱动归纳总结(五):2.操作硬件--IO内存 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  7. 菜鸟系列k8s——k8s集群部署(2)

    k8s集群部署 1. 角色分配 角色 IP 安装组件 k8s-master 10.0.0.170 kube-apiserver,kube-controller-manager,kube-schedul ...

  8. luoguP2822-组合数问题(基础DP)

    题目链接:https://www.luogu.org/problemnew/show/P2822 题意:输入T和k,有T组询问.每组询问输入n.m,求C(i,j)能模k的个数(0<=i<= ...

  9. [转帖]开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别

    开源许可证GPL.BSD.MIT.Mozilla.Apache和LGPL的区别 https://www.geek-workshop.com/thread-1860-1-1.html     liamj ...

  10. Oracle表概念

    对于初学者来说,对表的概念也有一定的认识.因为我们对数据库的操作,90%以上是对表的操作. 常见表的规则表(Regular table),严格意义上来说又叫 heap table(堆表),也就是我们最 ...