【状压dp】Most Powerful
[ZOJ3471]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
Author: GAO, Yuan
Contest: ZOJ Monthly, February 2011
题目大意:给你一些元素,这些元素碰撞后会产生其中消失的物品的能量(你可以自己决定哪一个消失),求最后剩1个元素的最大能量释放值
试题分析:比较基础的一道状压dp 貌似在dp中我只能说状压dp有基础题,好弱QAQ
dp[S]表示序列消到状态为S的时候的最大释放能量值。
可得出如下转移方程(因为是消失,所以应该从(1<<N)-1到1枚举): dp[S]=max(dp[S],dp[S+(1<<(k-1))]+make[j][k]);
其中k代表消失的能量,j代表剩下的能量,前提是S包含j,不包含k,k!=j
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,M;
int dp[MAXN];
int e[1001][1010]; int main(){
N=read();
while(1){
if(!N) break;
memset(dp,0,sizeof(dp));
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++)
e[i][j]=read();
}
int ans=0;
for(int i=(1<<N)-2;i>=1;i--){
for(int j=1;j<=N;j++){
if(!((i>>(j-1))&1)) continue;
for(int k=1;k<=N;k++){
if(k==j||((i>>(k-1))&1)) continue;
dp[i]=max(dp[i],dp[i+(1<<(k-1))]+e[j][k]);
}
}
}
for(int i=1;i<=N;i++) ans=max(ans,dp[1<<(i-1)]);
printf("%d\n",ans);
N=read();
}
}
【状压dp】Most Powerful的更多相关文章
- HDU 2809 God of War (状压DP)
God of War Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
- 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
随机推荐
- 关于RecylerView:1.在ScrollView的RecylerView滑动事件的处理。2.item之间的距离 小数取整
1.在ScrollView的RecylerView滑动事件的处理. 在布局文件中在RecylerView外包裹一层相对布局 2.RecylerView item之间的距离 (1)编写SpaceItem ...
- git 配置多用户
.ssh 下的 config.txt 内容 # 配置github.com Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa_ ...
- [Leetcode Week13]Palindrome Partitioning
Palindrome Partitioning 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/palindrome-partitioning/desc ...
- mips64高精度时钟引起ktime_get时间不准,导致饿狗故障原因分析【转】
转自:http://blog.csdn.net/chenyu105/article/details/7720162 重点关注关中断的情况.临时做了一个版本,在CPU 0上监控所有非0 CPU的时钟中断 ...
- [转载]关于python字典类型最疯狂的表达方式
一个Python字典表达式谜题 让我们探究一下下面这个晦涩的python字典表达式,以找出在python解释器的中未知的内部到底发生了什么. # 一个python谜题:这是一个秘密 # 这个表达式计算 ...
- 刷新SqlServer数据库中所有的视图
ALTER PROCEDURE sp_refallview AS --刷新所有视图 DECLARE @ViewName VARCHAR(MAX); DECLARE @i INT; ; DECLARE ...
- HDU 6183 Color it 线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6183 题意: 有四种操作: 0:清除所有点 1 x y c : 给点(x, y)添加一种颜色c(颜色不 ...
- Median_of_Two_Sorted_Arrays(理论支持和算法总结)
可以将这个题目推广到更naive的情况,找两个排序数组中的第K个最大值(第K个最小值). 1.直接 merge 两个数组,然后求中位数(第K个最大值或者第K个最小值),能过,不过复杂度是 O(n + ...
- POJ-1681
Painter's Problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4839 Accepted: 2350 ...
- graylog安装
官网:http://docs.graylog.org/en/2.4/pages/installation/os/centos.html Prerequisites Taking a minimal s ...