问题

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. Git本地初始化并推送到远程仓库

    git常用命令 1.全局配置git用户名邮箱 git config --global user.name '你的名字' git config --global user.email '你的邮箱地址' ...

  2. PJzhang:U盘容量变小后的恢复以及U盘加密

    猫宁!!! 参考链接:https://jingyan.baidu.com/article/8ebacdf0544ae049f65cd5da.html 我的一个U盘,16G,制作了deepin linu ...

  3. webhooks动态更新配置

    config server 项目中加入 monitor依赖 <dependency> <groupId>org.springframework.cloud</groupI ...

  4. 在依赖的框架中已经有统一异常处理的情况下,如何定制自己的统一异常处理spring boot版本

    spring boot 环境下的统一异常处理大家已经非常熟悉了,不熟悉的化可以参考 <<Spring Boot中Web应用的统一异常处理>>.公司内部的统一异常处理如下: @E ...

  5. docker随笔

    --查看系统内核版本,docker对于centos系统内核版本需要高于3.10uname -r--移除旧的版本sudo yum remove docker \ docker-client \ dock ...

  6. django中Queryset的删除问题、分页问题

    在开发选课界面时需要过滤掉已经选择过的课程,之前一直以为QuerySet是列表的结构,所以打算在判断之后使用list.remove()方法将已选的课程除掉,但在实际操作时,发现这么做并不行,原来Que ...

  7. sqlalchemy的常用字段

    #encoding: utf-8 from sqlalchemy import create_engine,Column,Integer,String,\ Float,Boolean,DECIMAL, ...

  8. nslookup 命令

    NAME nslookup - query Internet name servers interactively SYNOPSIS nslookup [-option] [name | -] [se ...

  9. 封装CURD

    <?php include ('ft.php'); $db=Danli::show(); //查询 //$re=$db->table('stree')->where(['name'= ...

  10. iView 发布后台管理系统 iview-admin

    简介 iView Admin 是基于 Vue.js,搭配使用 iView UI 组件库形成的一套后台集成解决方案,由 TalkingData 前端可视化团队部分成员开发维护.iView Admin 遵 ...