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 ;
}

zoj 3471(状态压缩DP,类似于点集配对)的更多相关文章

  1. zoj 3471(状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 dp[state]表示当前状态为state时的所能获得的最大值 ...

  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. poj 3311 Hie with the Pie(状态压缩dp)

    Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...

  5. Travelling(spfa+状态压缩dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ...

  6. Gym-101915D Largest Group 最大独立集 Or 状态压缩DP

    题面题意:给你N个男生,N个女生,男生与男生之间都是朋友,女生之间也是,再给你m个关系,告诉你哪些男女是朋友,最后问你最多选几个人出来,大家互相是朋友. N最多为20 题解:很显然就像二分图了,男生一 ...

  7. 状态压缩DP(大佬写的很好,转来看)

    奉上大佬博客 https://blog.csdn.net/accry/article/details/6607703 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的 ...

  8. hoj2662 状态压缩dp

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

  9. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

随机推荐

  1. bzoj 1500 [NOI 2005] 维修数列

    题目大意不多说了 貌似每个苦逼的acmer都要做一下这个splay树的模版题目吧 还是有很多操作的,估计够以后当模版了.... #include <cstdio> #include < ...

  2. [HDU3062]Party(2-sat)

    传送门 2-sat问题,只需要判断yes或no 所以可以直接连边,缩点,判断同一组的是否在同一个块中. #include <cstdio> #include <stack> # ...

  3. CodeVs1519 过路费

    题目描述 Description 在某个遥远的国家里,有 n个城市.编号为 1,2,3,…,n.这个国家的政府修建了m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市T需要收取的过路费 ...

  4. 收藏CSS经典技巧

    一. CSS字体属性简写规则 一般用CSS设定字体属性是这样做的: font-weight: bold; font- style: italic; font-varient: small-caps;  ...

  5. python学习之-- 装饰器

    高阶函数+嵌套函数 == 装饰器 什么是装饰器: 其实也是一个函数. 功能:为其他的函数添加附加功能 原则:不能修改被装饰的函数的源代码和调用方式 学习装饰器前首先要明白以下3条事项: 1:函数 即 ...

  6. C/C++ (一)

    c语言中的逻辑运算符都是短路运算,一旦能够确定整个表达式的值就不再计算,配合c的定义的灵活性,可以写出很多漂亮的程序. 例如 如果要在一个长为n的数列s中找到第k个没被标记过的数 for(i=1,j= ...

  7. POJ 1724 【存在附加约束的最短路问题】【优先队列】

    题意:给K个权值.给含有N个点,R条单向边的图. 每条边都有两个权值,其中一个路长,另外一个是附加权值. 要求路的附加权值之和不超过K的情况下求最短路. 思路: 自己的思路太狭隘,这题还是看了大牛的思 ...

  8. sql多表更新

    --sql多表更新update PMS_Financial_Gathering set ShouldMoney=PMS_Contract_RentScheme.Rentfrom PMS_Financi ...

  9. Eclipse的Servers视图中无法添加Tomcat6/Tomcat7

    原文:http://blog.csdn.net/blueheart20/article/details/40043749 问题的提出: 无法从以下方式,添加Tomcat服务器.  其中ServerNa ...

  10. 异步SOCKET分包和组包的一种通用算法

    unit uPackage;// 应用协议// cxg 2016-9-23// 包=包头+包体 interface uses SysUtils, Classes, PeachCtrl.Net.Iocp ...