zoj3471 Most Powerful
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
题意是有n个气球,n的范围是1~n,每两个气球碰撞都会产生一定的能量,并且有一个气球会消失,问最后剩下一个气球的时候最多能产生多少能量。可以用状态压缩,设0表示气体存在,1表示气体消失,状态转移方程dp[state]=max{dp[state],dp[state']+a[i][j]}.
#include<stdio.h>
#include<string.h>
int a[15][15],dp[1500];
int max(int a,int b){
return a>b?a:b;
}
int main()
{
int n,m,i,j,s,ans;
while(scanf("%d",&n)!=EOF && n!=0)
{
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
memset(dp,0,sizeof(dp));
for(s=1;s<(1<<n);s++){
for(j=1;j<=n;j++){
if(s&(1<<(j-1))){
for(i=1;i<=n;i++){
if( !(s&(1<<(i-1))) ){
dp[s]=max(dp[s],dp[s-(1<<(j-1))]+a[i][j]);
}
}
}
}
}
ans=0;
for(i=1;i<=n;i++){
if(dp[(1<<n)-1-(1<<(i-1))]>ans)ans=dp[(1<<n)-1-(1<<(i-1))];
}
printf("%d\n",ans);
}
return 0;
}
zoj3471 Most Powerful的更多相关文章
- ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)
Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...
- 【状压dp】Most Powerful
[ZOJ3471]Most Powerful Time Limit: 2 Seconds Memory Limit: 65536 KB Recently, researchers on Ma ...
- HDOJ 3593 The most powerful force
树形DP / 泛化物品的背包...可以去看09年徐持衡论文<浅谈几类背包问题> The most powerful force Time Limit: 16000/8000 MS (Jav ...
- CodeForces 86D Powerful array(莫队+优化)
D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...
- hdu 4150 Powerful Incantation
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4150 Powerful Incantation Description Some dangerous ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- 10+ powerful debugging tricks with Visual Studio
10+ powerful debugging tricks with Visual Studio Original link : http://www.codeproject.com/Articles ...
- zoj 3471 Most Powerful
题目链接:zoj 3471 Most Powerful 作者:jostree 转载请说明出处 很经典的状态dp,使用i的二进制位表示粒子的状态,0表示存在,1表示不存在.dp[i]表示在状态i的情况 ...
- zoj 3471 Most Powerful(状态压缩dp)
Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These ato ...
随机推荐
- Docker学习笔记之搭建Docker私有仓库
Docker仓库服务器名为Docker注册(registry)服务器.可以使用docker push命令将镜像上传到注册服务器,也可以使用docker pull命令下载服务器的镜像. Docker注册 ...
- Linux学习笔记 | docker基本命令
Docker的三大核心概念:镜像.容器.仓库 镜像:类似虚拟机的镜像.用俗话说就是安装文件. 容器:类似一个轻量级的沙箱,容器是从镜像创建应用运行实例,可以将其启动.开始.停止.删除.而这些容器都是相 ...
- AvaloniaUI体验
公司的原有的PC端WPF产品有跨平台需求,无奈微软自己的xamarin对wpf的支持当前尚未达到能支撑产品的成熟度,于是经过搜索,发现了一个号称用WPF实现跨平台的第三方图形库AvaloniaUI. ...
- 18.java设计模式之中介者模式
基本需求 智能家庭包括各种设备,闹钟.咖啡机.电视机.窗帘等 要看电视时,各个设备可以协同工作,自动完成看电视的准备工作,比如流程为:闹铃响起->咖啡机开始做咖啡->窗帘自动落下-> ...
- 【Linux】Linux基础命令 - 目录相关的命令 ls 、cd、du
文章目录 目录相关的命令 ls 命令:列出文件和目录 cd 命令:切换目录 du 命令:显示目录包含的文件大小 总结 参考资料 巩固和复习Linux系统基础命令知识 目录相关的命令 ls 命令:列出文 ...
- 【Spring】Spring中的Bean - 4、Bean的生命周期
Bean的生命周期 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 了解Spring中Bean的生命周期有何意义? 了解Sp ...
- /etc/hosts导致的问题
今天安装完成orzdba之后,执行./orzdba -l 报如下错误: Usage: Socket::inet_ntoa(ip_address_sv) at /var/lib/mysql/trunk/ ...
- ctfhub技能树—sql注入—时间盲注
打开靶机 查看页面信息 测试时间盲注 可以看到在执行命令后会有一定时间的等待,确定为时间盲注 直接上脚本 1 #! /usr/bin/env python 2 # _*_ coding:utf-8 _ ...
- service自动发现,yaml文件管理内外部端口访问
service服务发现 [root@k8s-master ~]# vim busybox-5d4f595646-dzjv4.yaml apiVersion: v1 kind: Pod metadata ...
- 基于Dockfile构建JAVA环境网站镜像
查看本地目录 [root@docker tomcat]# ls apache-tomcat-8.5.16.tar.gz Dockerfile jdk-8u91-linux-x64.tar.gz ...