题目链接

题目

题目描述

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\) )满足状态中用过,于是有转移方程:

\[dp[st] = max(dp[st], dp[st \wedge (1 << (j - 1))] + g[i][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的更多相关文章

  1. HDOJ 3593 The most powerful force

    树形DP / 泛化物品的背包...可以去看09年徐持衡论文<浅谈几类背包问题> The most powerful force Time Limit: 16000/8000 MS (Jav ...

  2. CodeForces 86D Powerful array(莫队+优化)

    D. Powerful array time limit per test 5 seconds memory limit per test 256 megabytes input standard i ...

  3. hdu 4150 Powerful Incantation

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4150 Powerful Incantation Description Some dangerous ...

  4. D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力

    莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...

  5. 10+ powerful debugging tricks with Visual Studio

    10+ powerful debugging tricks with Visual Studio Original link : http://www.codeproject.com/Articles ...

  6. zoj 3471 Most Powerful

    题目链接:zoj 3471 Most Powerful  作者:jostree 转载请说明出处 很经典的状态dp,使用i的二进制位表示粒子的状态,0表示存在,1表示不存在.dp[i]表示在状态i的情况 ...

  7. zoj 3471 Most Powerful(状态压缩dp)

    Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These ato ...

  8. codefroce D. Powerful array[初识块状数组]

    codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...

  9. D. Powerful array

    D. Powerful array 题意 给定一个数列:a[i] (1<= i <= n) K[j]表示 在区间 [l,r]中j出现的次数.有t个查询,每个查询l,r,对区间内所有a[i] ...

  10. 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. ...

随机推荐

  1. ReentrantLock 可重入锁总结

    本文为博主原创,未经允许不得转载: ReentrantLock 是一种内置锁,也叫可重入锁(ReentrantLock),它允许线程再次获取已持有的同步锁,这样防止死锁的发生.在使用Reentrant ...

  2. 【C++】类成员冒号初始化以及构造函数内赋值

    From:https://blog.csdn.net/zj510/article/details/8135556 通常我们对类成员进行"初始化"有两种方式: 1. 构造函数后面跟冒 ...

  3. springboot封装统一返回

    springboot返回统一的标准格式 定义注解 package com.yaoling.annotation; import java.lang.annotation.*; @Target({Ele ...

  4. [转帖]麒麟v10上部署TiDBv5.1.2生产环境的最佳实践

    https://tidb.net/book/tidb-monthly/2022/2022-07/usercase/tidb-v5-1-2 前言​ 笔者最近在一个银行项目中做 PoC 测试,由于客户选择 ...

  5. [转帖]Jmeter之界面语言设置

    https://developer.aliyun.com/article/1173114#:~:text=%E6%B0%B8%E4%B9%85%E6%80%A7%E8%AE%BE%E7%BD%AE%E ...

  6. Docker 镜像减少体积的思路和方法

    Docker 镜像减少体积的思路和方法 背景 有一个项目感觉镜像有点大 这边同事喊着一起帮忙处理一下. 今天基本上就在客户现场进行处理了. 想着应该把自己想到的东西整理一下. 整体思路 1. 清理do ...

  7. [转帖]GC Ergonomics间接引发的锁等待超时问题排查分析

    https://www.cnblogs.com/micrari/p/8831834.html 1. 问题背景 上周线上某模块出现锁等待超时,如下图所示:我虽然不是该模块负责人,但出于好奇,也一起帮忙排 ...

  8. 是否开启raid卡缓存的影响

    开启raid卡缓存 Write back 对IO性能的影响 背景 公司买了一台服务器. 想进行一下升级 但是因为管理员担心数据丢失, 使用了write through + (raid6 + hotsp ...

  9. Postgresql 数据库设置备份以及简单清理磁盘空间和wal日志的方法

    1. 最近想简单的进行数据库的备份工作, 因为现在数据库主要是用的pg数据库 , 所以想到用文本的方式进行, 有清理了一下日志表的数据 这里一起记录一下. 先记录一下查看比较大的表的信息. 从网上找了 ...

  10. 记一次flex布局中子项目尺寸不受flex-shrink限制的问题

    预期是写一个如下所示的布局内容: 即有一个固定高度的外部容器,顶部的header已知高度,在header占据了固定高度后,剩下的都分给body部分.因此采用flex布局,header设置flex-sh ...