【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标 ...
随机推荐
- cordova微信支付回调App闪退
这是cordova版本太高,不兼容这个插件所导致的.解决方案是修改$your_project/plugins/cordova-plugin-wechat/scripts/android-install ...
- python爬虫之scrapy的pipeline的使用
scrapy的pipeline是一个非常重要的模块,主要作用是将return的items写入到数据库.文件等持久化模块,下面我们就简单的了解一下pipelines的用法. 案例一: items池 cl ...
- 老男孩python学习自修第二十四天【多进程】
1. 体验多进程的运行速度 #!/usr/bin/env python # _*_ coding:UTF-8 _*_ from multiprocessing import Pool import t ...
- python web需要了解哪些
1. socket.tcp/ip.http(cookie.session.token).https.ssl 2. wsgi:https://www.python.org/dev/peps/pep-33 ...
- Linux下 rewrite_mod 的配置
以下使用最新的 Ubuntu 16.04 测试; 安装好apache后先确认有没有rewrite模块,大多数情况下是有的:ls /etc/apache2/mods-available |grep re ...
- js函数使用prototype和不适用prototype的区别
js中类定义函数时用prototype与不用的区别 原创 2017年06月05日 12:25:41 标签: 函数 / prototype / class 首先来看一个实例: function Li ...
- How to remove popup on boot on Windows 2003
Administrative Tools\Manage Your Server\Add or remove a role\Add or Remove Programs Local Computer P ...
- 开源 DotNetty 实现的 Modbus TCP/IP 协议
本项目的目的是为了学习 DotNetty 与 Modbus 协议,参考 modjn 实现功能 0x01: Read Coils (读取线圈/离散量输出状态) 0x02: Read Discrete I ...
- Android Studio导入jar包
使用开源框架是,可以直接复制源代码到自己的项目(本人在Android Studio中操作报R程序包不存在),也可以使用jar包,下面记录一下今天使用SmartImageView.jar的过程,不记录S ...
- 【XSY2708】hack 网络流
题目描述 给你一个图,每条边有一个权值.要求你选一些边,满足对于每条从\(1\)到\(n\)的路径上(可以不是简单路径)有且仅有一条被选中的边.问你选择的边的边权和最小值. \(n\leq 100\) ...