ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)
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 && 状态压缩 && 记忆化搜索 && 位运算)的更多相关文章
- loj 1011(状态压缩+记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25837 思路:状态压缩+记忆化搜索. #include<io ...
- HDU 4628 Pieces(状态压缩+记忆化搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=4628 题意:给个字符窜,每步都可以删除一个字符窜,问最少用多少步可以删除一个字符窜分析:状态压缩+记忆化搜索 ...
- Doing Homework---hdu1074(状态压缩&&记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 有n(n<=15)门课需要做作业,每门课所需时间是used_time以及每门课作业上交的最 ...
- light oj 1011 - Marriage Ceremonies (状态压缩+记忆化搜索)
题目链接 大概题意是有n个男的n个女的(原谅我这么说,我是粗人),给你一个n*n的矩阵,第i行第j列表示第i个女(男)对第j个男(女)的好感度,然后要安排n对相亲,保证都是正常的(无搞基百合之类的), ...
- GYM 101933E 状态压缩 + 记忆化搜索
题意:我方有n个士兵,敌方有m个,每方士兵都有一个血量,现在有k轮无差别炮火打击,每次都会从存活的士兵中随机选一人,这名士兵的HP就-1,问对方被团灭的概率有多大? 思路:因为n和m的范围很小,我们可 ...
- NYOJ16|嵌套矩形|DP|DAG模型|记忆化搜索
矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a& ...
- ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- poj1191 棋盘分割【区间DP】【记忆化搜索】
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16263 Accepted: 5812 Description ...
- 状态压缩中常用的位运算(DP)
面对位运算,一直很无感...可能数学太差,脑洞太小. 1.首先是最基本的: 与&,或|,非~,异或^. 2.获取一个或者多个固定位的值: 假设 x = 1010(二进制),我们要取左数第二位的 ...
随机推荐
- Lua学习五----------Lua循环
© 版权声明:本文为博主原创文章,转载请注明出处 1.循环类型 1.1 while循环 - 语法:while(condition) do ...<执行语句> end - 解析:判断cond ...
- 基于JQuery实现表单元素值的回写
form.jsp: <%@ page language="java" import="java.util.*" pageEncoding="GB ...
- HBase 系统架构及数据结构
一.基本概念 2.1 Row Key (行键) 2.2 Column Family(列族) 2.3 Column Qualifier (列限定符) 2.4 Column ...
- ASP.NET页面间传值的几种方式
ASP.NET页面间传值的几种方式 1.使用QueryString 使用QuerySting在页面间传递值已经是一种很老的机制了,这种方法的主要优点是实现起来非常简单,然而它的缺点是传递的值是会显示在 ...
- A、B两伙马贼意外地在一片沙漠中发现了一处金矿,双方都想独占金矿,但各自的实力都不足以吞下对方,经过谈判后,双方同意用一个公平的方式来处理这片金矿。处理的规则如下:他们把整个金矿分成n段,由A、B开始轮流从最左端或最右端占据一段,直到分完为止。 马贼A想提前知道他们能分到多少金子,因此请你帮忙计算他们最后各自拥有多少金子?(两伙马贼均会采取对己方有利的策略)
第一种做法:这种方法,算法复杂性大,重复的递归 #include "stdafx.h" #include<iostream> #include<vector> ...
- 记录下关于ejabberd及XMPP的官网链接
ejabberd中文翻译 ——http://wiki.jabbercn.org/Ejabberd2:安装和操作指南 XMPP中文翻译: http://wiki.jabbercn.org/XEP-012 ...
- nginx教程2:日志
主要有两种:access_log(访问日志) 和 error_log(错误日志). access_log 访问日志 access_log 主要记录客户端访问 Nginx 的每一个请求,格式可以自定义. ...
- 09-redis事务及锁应用
Redis 中的事务 Redis支持简单的事务 Redis与 mysql事务的对比 ------------------------------------------------------- My ...
- 1213 - Deadlock found when trying to get lock; try restarting transaction
1213 - Deadlock found when trying to get lock; try restarting transaction 出现这个原因要记住一点就是:innodb的行锁 和解 ...
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal not in range(12
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...