Description

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

这个题目对于每个粒子有存在和消失两种状态,自然总的状态数有2^n种。

对于某种状态,如果用普通的表示,自然有dp[0][1][0]……[1](其中1表示存在,0表示消失),因为每个粒子只有0和1两种值,此处采用二进制进行状态压缩。

接下来考虑对于某个状态10001011,如果第一1要变成0,自然是考虑这个粒子和现存的粒子进行碰撞,然后找能达到的最大值。我们记这个值为GetMax(state, now)(其中state是当前二进制状态,now是由1转为0的那一位)。

于是考虑状态转移方程,对于状态10001,必然由状态11001或10101或10011转换而来,自然对于state,可以由某个0位变成1的状态forestate转换而来。所以:

dp[state] = max{dp[forestate] + GetMax(forestate, now)}

此处采用记忆化搜索进行求解。初始状态M = (1<<n)^0。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> using namespace std; int n, M;
int p[15][15];
int dp[1500]; void Input()
{
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
scanf("%d", &p[i][j]);
M = (M<<n)^0;
} int GetMax(int state, int now)
{
int Max = 0;
for (int i = 0; i < n; ++i)
if (state&(1<<i))
Max = max(Max, p[i+1][now]);
return Max;
} void Dfs(int state)
{
if (state == M)
{
dp[state] = 0;
return;
}
int Max = 0, t;
for (int i = 0; i < n; ++i)
{
if ((state&(1<<i)) == 0)
{
if (dp[state|(1<<i)] == 0)
Dfs(state|(1<<i));
t = dp[state|(1<<i)] + GetMax(state, i+1);
Max = max(Max, t);
}
}
dp[state] = Max;
} int Work()
{
int ans = 0;
for (int i = 0; i < n; ++i)
{
Dfs(1<<i);
ans = max(ans, dp[1<<i]);
}
return ans;
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF && n)
{
Input();
printf("%d\n", Work());
}
return 0;
}

ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)的更多相关文章

  1. loj 1011(状态压缩+记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25837 思路:状态压缩+记忆化搜索. #include<io ...

  2. HDU 4628 Pieces(状态压缩+记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索  ...

  3. Doing Homework---hdu1074(状态压缩&&记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 有n(n<=15)门课需要做作业,每门课所需时间是used_time以及每门课作业上交的最 ...

  4. light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)

    题目链接 大概题意是有n个男的n个女的(原谅我这么说,我是粗人),给你一个n*n的矩阵,第i行第j列表示第i个女(男)对第j个男(女)的好感度,然后要安排n对相亲,保证都是正常的(无搞基百合之类的), ...

  5. GYM 101933E 状态压缩 + 记忆化搜索

    题意:我方有n个士兵,敌方有m个,每方士兵都有一个血量,现在有k轮无差别炮火打击,每次都会从存活的士兵中随机选一人,这名士兵的HP就-1,问对方被团灭的概率有多大? 思路:因为n和m的范围很小,我们可 ...

  6. NYOJ16|嵌套矩形|DP|DAG模型|记忆化搜索

    矩形嵌套 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a& ...

  7. ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  8. poj1191 棋盘分割【区间DP】【记忆化搜索】

    棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16263   Accepted: 5812 Description ...

  9. 状态压缩中常用的位运算(DP)

    面对位运算,一直很无感...可能数学太差,脑洞太小. 1.首先是最基本的: 与&,或|,非~,异或^. 2.获取一个或者多个固定位的值: 假设 x = 1010(二进制),我们要取左数第二位的 ...

随机推荐

  1. vue-bus 组件通信插件

    vue-bus 一个 Vue.js 事件中心插件,同时支持 Vue 1.0 和 2.0 原因 Vue 2.0 重新梳理了事件系统,因为基于组件树结构的事件流方式实在是让人难以理解,并且在组件结构扩展的 ...

  2. 【Python基础】之函数、类和方法

    一.函数 1. def定义函数 Python Shell: def add(a,b): return a+b >>>add(1,2) 3 def add(a=1,b=2): retu ...

  3. JVM完全指南

    JVM完全指南     一:虚拟机内存图解   JAVA程序运行与虚拟机之上,运行时需要内存空间.虚拟机执行JAVA程序的过程中会把它管理的内存划分为不同的数据区域方便管理.     虚拟机管理内存数 ...

  4. Jenkins:使用Git Parameter插件实现tag或分支的选择性构建

    Jenkins如何选择任意一个tag来构建代码 Jenkins如何选择任意一个branch(分支)来构建代码 苦恼了一段时间后,发现Git Parameter插件实现可以帮助我们来实现. 下面来介绍如 ...

  5. 20-ab压力测试及nginx性能统计模块

    一:找到apache ab模块. ab -c 1000 -n 50000 http://127.0.0.1/index.html 查看信息: 超过1024个线程 出现错误,说打开文件太多了.cket: ...

  6. 阿里云Opensearch数据类型

    阿里云主要支持以下数据类型,详情参考:https://help.aliyun.com/document_detail/29121.html 类型 说明 INT int64整型 INT_ARRAY in ...

  7. 用Newtonsoft将json串转为对象的方法(详解)

    首先,将json串转为一个JObject对象: JObject jo = (JObject)JsonConvert.DeserializeObject(CurrentSelectedItemReq) ...

  8. ios何时使用self.

     本文转载至  http://blog.csdn.net/lvxiangan/article/details/27204265   何时使用self.在网上搜索或者论坛里的回复大多都是简简单单的说这与 ...

  9. IOS GameCenter验证登陆

    #import "GameKitHelper.h" #import "GameConstants.h" @interface GameKitHelper () ...

  10. 大白第一章第四节dp例题

    入口 UVALive - 3882 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ //f[i]表示 ...