【CH0103】最短哈密顿路径
题目大意:给定一个 N 个点的无向图,点有点权,求从 0 号点走到 N-1 号点的最短哈密顿路径是多少。
题解:由于哈密顿路径的定义是每个顶点必须经过且仅能经过一次,因此,可用当前是否经过了这些点和当前在哪个点来表示出一个状态,则一共有 \(O(n2^n)\) 个状态。考虑转移方式,对于在 j 这个点的情况来说,可以从以下与 j 相邻的节点 k 转移来,k 必须满足在当前状态已经走到。因此,时间复杂度为 \(O(n^22^n)\)。
update at 2019.3.29
注意,由于状态转移方程为 $$dp[i][S]=min{dp[k][S']+dis(i,k)},k\in [1,n],S'\subset S$$,从中可以看出 i 的决策是不具有阶段性的,即:i 并不是从小到大进行的,而对于集合来说是从小到大进行转移的,因此应该以走过的点的集合作为阶段,即:集合应该是循环的第一层。今天被这个搞到自闭了。。QAQ
代码如下
#include <bits/stdc++.h>
using namespace std;
int dp[1<<20][20];
int n,G[20][20];
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&G[i][j]);
memset(dp,0x3f,sizeof(dp));
dp[1][0]=0;
for(int i=1;i<1<<n;i++)
for(int j=0;j<n;j++)if(i>>j&1)
for(int k=0;k<n;k++)if((i^1<<j)>>k&1)
dp[i][j]=min(dp[i][j],dp[i^1<<j][k]+G[k][j]);
printf("%d\n",dp[(1<<n)-1][n-1]);
return 0;
}
【CH0103】最短哈密顿路径的更多相关文章
- ural 1143. Electric Path(凸包上最短哈密顿路径)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1143 题意:逆时针给一个凸包的n(n<=200)个顶点坐标,求一个最短哈密顿路径的 ...
- CH0103最短Hamilton路径 & poj2288 Islands and Brigdes【状压DP】
虐狗宝典学习笔记: 取出整数\(n\)在二进制表示下的第\(k\)位 \((n >> ...
- CH0103 最短Hamilton路径 dp
正解:状压dp 解题报告: 完了吃枣退役:D 我是真的没想到这是个dp...脑子越来越不好了,大概是太久没碰OI了都要生疏了...哭了,感觉自己太傻逼了可能不适合学信息... 知道是个状压dp就eas ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- Python正则表达式:最短匹配
最短匹配应用于:假如有一段文本,你只想匹配最短的可能,而不是最长. 例子 比如有一段html片段,'\this is first label\\the second label\',如何匹配出每个a标 ...
随机推荐
- node 模块化思想中index.js的重要性
目录结构如上图 module1和modlue2.main在同一级 module1下文件: index.js var test2=require('./test2'); var sayHi=functi ...
- 在使用IWMS的时候,IWMS自带函数样式无法满足我们需求。以下一段JS是实现左图右字的适用于IWMS的代码。
<div class="wz-list">里边需要有html做好的Html代码样式</div> <script> var attrnew = & ...
- endnote插入文献时出现{,#}这样的乱码
1)在每次插入文献后,再点击一下Bibliography里面的Update Citation and Bibliography即可: (2)较好的解决方法也较为简单,只需要再一次进入Word中的End ...
- 法语Linux NuTyX 11 RC2 发布
读 NuTyX是一个法语Linux发行版(具有多语言支持),由Linux From Scratch和Beyond Linux From Scratch构建,带有一个名为“cards”的自定义包管理器. ...
- 【RNN】资源汇总
wesome Recurrent Neural Networks A curated list of resources dedicated to recurrent neural networks ...
- 如何创建djiago项目和djiago连接数据库
介绍 主要介绍在python中如何使用pycharm创建djiago项目以及如何将djiago项目和mysal数据库连接起来 创建djiago项目 1.使用pycharm创建djiao项目 点击pyc ...
- HTML5-Video视频-基础篇
展示视频 视频 <video width=" controls="controls"> <source src="movie.mp4" ...
- Bootstrap modal 模态框垂直居中显示补丁
<script> $.fn.modal.Constructor.prototype.adjustDialog1 = function(){ var modalIsOverflowing = ...
- Codeforces Round #539 Div. 1
A:即求长度为偶数的异或和为0的区间个数,对前缀异或和用桶记录即可. #include<iostream> #include<cstdio> #include<cmath ...
- Android InputType
转载: http://blog.csdn.net/wei_zhi/article/details/50094503 在Android开发过程中,我们经常使用到EditText控件,并且会根据各种需求设 ...