Most Powerful


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

解法1:

所有的原子组成一个集合,每次从中选取两个点,选择一个攻击点,选择一个被攻击点,

d[s]=max(d[s],d[s ^ (1<<j)] +a[i][j]);

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define maxn 10
using namespace std;
int d[<<]; //表示到达状态s时产生的最大能量
int a[][];
int n;
void init()
{
memset(d,,sizeof(d));
}
void solve()
{
for(int s=;s<(<<n);s++)
{
d[s]=;
for(int i=;i<n;i++)
if(s & (<<i))
{
for(int j=;j<n;j++)
if(s & (<<j))
{
if(i==j)
continue;
d[s]=max(d[s],d[s ^ (<<j)]+a[i][j]); }
}
} }
int main()
{
while(~scanf("%d",&n))
{
if(n==)
break;
init();
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
scanf("%d",&a[i][j]);
}
solve();
int ans=;
// for(int i=0;i<(1<<n);i++)
// cout<<d[i]<<" ";
//cout<<endl;
//ans=max(ans,d[i]);
printf("%d\n",d[(<<n)-]);
}
return ;
}

解法2:

假设一个数,第i位表示第i个原子是否被灭掉,如果被灭掉则为1,没被灭掉为0,那么所有状态都可以用2^n范围内的数来表示。则初始状态为0,即所有原子都没有消失

  令dp[i]表示达到状态 i 时所产生的最大能量,则答案就是从0~(1<<n)所有状态里释放的最大的那个能量。 需要枚举所有状态。

  假设当前状态是s,从1~n里边枚举主动碰撞的原子 i ,和被动碰撞被消灭掉的原子 j ,则

  dp[s | (1<<j)] = max{dp[s | (1<<j)] , dp[s] + A[i][j]};

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define maxn 10
using namespace std;
int d[<<]; //表示到达状态s时产生的最大能量
int a[][];
int n;
void init()
{
memset(d,,sizeof(d));
}
void solve()
{
for(int s=;s<(<<n);s++)
{
for(int i=;i<n;i++)
{
if( ! (s & (<<i)) ) //不知为何写成(if( (s & (1 << i)) ))也能过
{
for(int j=;j<n;j++)
{
if(i==j)
continue;
if(s & (<<j))
continue;
d[s |(<<j)] = max(d[s | (<<j)],d[s]+a[i][j]);
}
}
}
} }
int main()
{
while(~scanf("%d",&n))
{
if(n==)
break;
init();
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
scanf("%d",&a[i][j]);
}
solve();
int ans=;
for(int i=;i<(<<n);i++)
ans=max(ans,d[i]);
printf("%d\n",ans); }
return ;
}

最新文章

  1. iOS应用性能调优好文mark
  2. 如何在开机时让Tomcat以进程的方式启动
  3. SQL语句在查询分析器中可以执行,代码中不能执行
  4. iPhone开发视频教程 Objective-C部分 (51课时)
  5. 常用的STL查找算法
  6. Windows脚本
  7. 用soaplib的django webserver
  8. Strategic game(POJ 1463 树形DP)
  9. 使用NPOI将数据库里信息导出Excel表格并提示用户下载
  10. Swift 学习Using Swift mix and match, network: 写rss读者
  11. 结构-行为-样式-Js函数节流
  12. 为什么大多数培训机构还停留在只教ssh框架?
  13. MySQL 2006 超时
  14. THUSC2016 游记
  15. C语言引用另一个源文件中定义的数组
  16. springBoot集成redisCluster
  17. python--使用队列结构来模拟烫手山芋的游戏
  18. 在linux系统下运行jar包的命令如下
  19. 引用全局变量global
  20. 【Error】:svnrdump: E130003: The XML response contains invalid XML

热门文章

  1. Monkey King(左偏树)
  2. 【贪心】codeforces B. Heidi and Library (medium)
  3. bzoj1086 [SCOI2005]王室联邦 树分块
  4. Lucene、Compass学习以及与SSH的整合
  5. 数据库备份与还原c#.net实现
  6. linux下库文件的编程
  7. php 基础复习 2018-06-18
  8. 三、fs文件操作模块
  9. BZOJ——T 1707: [Usaco2007 Nov]tanning分配防晒霜
  10. T1365 浴火银河星际跳跃 codevs