NC15832 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.
输入描述
There are multiplecases. The first line of each case has an integer N (2 <= N <= 10), whichmeans there are N atoms: A1, A2, ... , AN.Then N lines follow. There are N integers in each line. The j-th integer on thei-th line is the power produced when Ai and Aj collidewith Aj gone. All integers are positive and not larger than 10000.The last case isfollowed by a 0 in one line.There will be no morethan 500 cases including no more than 50 large cases that N is 10.
输出描述
Output the maximalpower these N atoms can produce in a line for each case.
示例1
输入
2
0 4
1 0
3
0 20 1
12 0 1
1 10 0
0
输出
4
22
题解
知识点:状压dp。
经典的TSP问题。这道题由于可以随便找一个 \(A\) 撞随便一个 \(B\) ,所以没必要记录上一个原子是啥,设 \(dp[st]\) 表示状态 \(st\) 的最大值。由于 \(A\) 撞 \(B\) 只会消失 \(B\) 并得到 \((A,B)\) 的能量,因此 \(A\) (编号 \(i\) )要满足状态中没用过,\(B\) (编号 \(j\) )满足状态中用过,于是有转移方程:
\]
用记忆化搜索也能写。
时间复杂度 \(O(n^22^n)\)
空间复杂度 \(O(2^n)\)
代码
#include <bits/stdc++.h>
using namespace std;
int n;
int g[17][17];
int dp[1 << 11];
int dfs(int st) {
if (~dp[st]) return dp[st];
for (int i = 1;i <= n;i++) {
if ((st >> (i - 1)) & 1) continue;
for (int j = 1;j <= n;j++) {
if (!((st >> (j - 1)) & 1)) continue;
dp[st] = max(dp[st], dfs(st ^ (1 << (j - 1))) + g[i][j]);
}
}
return dp[st];
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
while (cin >> n, n) {
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
cin >> g[i][j];
memset(dp, -1, sizeof(dp));
int ans = 0;
dp[0] = 0;
for (int i = 1;i <= n;i++) {
ans = max(ans, dfs(((1 << n) - 1) ^ (1 << (i - 1))));
}
cout << ans << '\n';
}
return 0;
}
NC15832 Most Powerful的更多相关文章
- 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 ...
- codefroce D. Powerful array[初识块状数组]
codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...
- D. Powerful array
D. Powerful array 题意 给定一个数列:a[i] (1<= i <= n) K[j]表示 在区间 [l,r]中j出现的次数.有t个查询,每个查询l,r,对区间内所有a[i] ...
- The new powerful SQL executing schedule monthly or weekly in DB Query Analyzer 7.01
1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainland. ...
随机推荐
- 基于html5开发的Win12网页版,抢先体验
据 MSPoweruser 报道,Windows 11虽然刚刚开始步入正轨,但最新爆料称微软已经在开启下一个计划,Windows 12 的开发将在 去年3 月份开始.德国科技网站 Deskmodder ...
- 函数指针、std::function、std::bind
函数指针.std::function.std::bind 函数指针: C++语法中可以直接将函数名作为指针, void fun(int a, int b); 在这个函数声明中,函数指针即为fun,传入 ...
- 左值,右值,引用,指针,常量,auto如何组合?
左值,右值,引用,指针,常量,auto如何组合? 左值引用:int &a = b; 左值引用是通过使用&符号来声明的,例如int &a. 左值引用用于绑定到左值(可标识的.持久 ...
- VMto阿里云的简单过程
VMto阿里云的简单过程 第一步打开网站 https://smcnext.console.aliyun.com/sourceServers/importMigrationSource?spm=5176 ...
- Redis不同版本性能研究
Redis不同版本性能研究 背景 前期同事遇到了一个大key的慢查询. 前提条件是: 一个 60万key的环境里面. 有一个 260万元素的set类型的key 产品经常会进行 smember key ...
- [转帖]ldconfig命令
https://linux265.com/course/linux-command-ldconfig.html ldconfig命令的作用主要是在默认搜寻目录/lib和/usr/lib以及动态库配置文 ...
- [转帖]Innodb存储引擎-idb文件格式解析
文章目录 ibd 文件格式解析 idb文件 page类型和格式(File Header & Trailer) FIL_PAGE_TYPE_FSP_HDR 格式 Extent Descripto ...
- [转帖]阿里发布自研ARM服务器芯片倚天710,不对外出售
https://www.modb.pro/db/139440 10月19日,阿里巴巴旗下半导体公司平头哥发布自研云芯片倚天710,并宣称该芯片是业界性能最强的ARM服务器芯片,性能超过业界标杆20%, ...
- 一个Redis dump文件的简要分析过程
摘要 遇到一个老大难的问题. 让帮忙分析一下一个Redis的dump文件. 虽然之前写过了rdb和rdr的文档 但是感觉大家都喜欢拿来主义. 没办法. 今天继续进行深入一点的分析. 原理其实还是基于r ...
- [转贴]英特尔Sapphire Rapids至强可扩展CPU完整型号爆料与路线图展望
2022-10-13 15:15· 稿源: cnbeta 腾讯云服务器促销:2核2G首年仅需40元 历史新低 @结城安穗-YuuKi_AnS 刚刚在社交媒体上,分享了与英特尔下一代 Sapph ...