[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的更多相关文章

  1. HDU 2809 God of War (状压DP)

    God of War Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  3. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  4. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  5. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  6. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  7. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  8. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

  9. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

随机推荐

  1. 【HNOI】 期望面积

    [题目描述]给定n个点,求这n个点组成凸包的期望面积.保证任意三点不共线. [数据范围]n<=100. 首先我们知道凸包面积的计算为所有在凸包上相邻的点的叉积和,那么我们可以枚举两个点,然后求出 ...

  2. js_!和!!的使用

    js中有些特殊的数据(“” 0 null undefined NaN),请求后台返回的数据中往往都有一些这样的数据,需要对这些数据进行过滤. 过滤代码 var a = 0;//0 "&quo ...

  3. docker 环境

    1.docker 升级 步骤: docker -v service docker stop yum -y update docker.io

  4. bzoj 1854 游戏 二分图匹配 || 并查集

    题目链接 Description lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的 ...

  5. 【Python学习笔记】使用Python计算皮尔逊相关系数

    源代码不记得是哪里获取的了,侵删.此处博客仅作为自己笔记学习. def multipl(a,b): sumofab=0.0 for i in range(len(a)): temp=a[i]*b[i] ...

  6. 直接在注册DB服务的时候,做beforeQuery事件监听

  7. Xmind 8 update5 破解

    Step 1. Download XMind Step 2. Run XMind at least once after installation, xmind will init the confi ...

  8. Git常规配置与基本用法

    Git环境配置 一. 全局配置 1. 配置文件 git全局配置文件.gitconfig默认在当前系统用户文件夹下,window可运行%USERPROFILE%查找,Mac系统在cd ~查找. 具体配置 ...

  9. (转)LSI SAS 1068E Raid CentOS 5.5 安装实例浪潮NF5220系列 分类: linux

    新来了一批服务器,全都是清一色的国产服务器,相同的阵列卡,令人头疼的是Linux标准内核不包含该raid驱动,需要单独安装,如果是新升级内核,肯定需要编译进去该raid驱动.一.先把主板自带的驱动光盘 ...

  10. jquery获取元素索引值index()的例子

    如果参数是一组DOM元素或者jQuery对象,那么返回值就是传递的元素相对于原先集合的位置. 如果参数是一个选择器,那么返回值就是原先元素相对于选择器匹配元素中的位置.如果找不到匹配的元素,则返回-1 ...