题目链接

题意

给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点)。

思路

因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间的最短路径。

接下来就是状压dp的部分。

将已经经过的点的状态用\(state\)表示,

则\(dp[state][k]\)表示当前到达点\(k\)后状态为\(state\)时的最短路径长度。

\[ans=min_{i=1}^{n}(dp[(1<<n)-1][i]+dis[i][0])
\]

可用记忆化搜索

Code

#include <cstdio>
#include <iostream>
#include <cstring>
#include <climits>
#define F(i, a, b) for (int i = (a); i < (b); ++i)
#define F2(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i > (b); --i)
#define dF2(i, a, b) for (int i = (a); i >= (b); --i)
#define maxn 12
#define maxs 1100
using namespace std;
typedef long long LL;
int n, a[maxn][maxn], dp[maxs][maxn];
bool vis[maxs][maxn];
void floyd() {
F2(k, 0, n) {
F2(i, 0, n) {
F2(j, 0, n) {
if (i==j||i==k||j==k) continue;
a[i][j] = min(a[i][j],a[i][k]+a[k][j]);
}
}
}
}
int dfs(int state, int p) {
if (state==(1<<(p-1))) return dp[state][p] = a[0][p];
if (vis[state][p]) return dp[state][p];
vis[state][p] = true;
int ans = INT_MAX, sta = state&~(1<<(p-1));
F2(i, 1, n) {
if (state&(1<<(i-1)) && i!=p) {
ans = min(ans, dfs(sta, i)+a[i][p]);
}
}
return dp[state][p] = ans;
}
void work() {
memset(dp, 0, sizeof dp);
memset(vis, 0, sizeof vis);
F2(i, 0, n) {
F2(j, 0, n) {
scanf("%d", &a[i][j]);
}
}
floyd();
int ans = INT_MAX;
F2(i, 1, n) ans = min(ans, dfs((1<<n)-1, i)+a[i][0]);
printf("%d\n", ans);
}
int main() {
while (scanf("%d", &n) != EOF && n) work();
return 0;
}

poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp的更多相关文章

  1. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  2. Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)

    题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...

  3. POJ 3311 Hie with the Pie(状压DP + Floyd)

    题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...

  4. poj 3311 Hie with the Pie dp+状压

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 ...

  5. poj 3311 Hie with the Pie (TSP问题)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 ...

  6. POJ 3311 Hie with the Pie 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

  7. POJ 3311 Hie with the Pie(DP状态压缩+最短路径)

    题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...

  8. [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法

    主题连接:  id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...

  9. POJ 3311 Hie with the Pie floyd+状压DP

    链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...

随机推荐

  1. 第2 章Python 语言基础

    必背必记 1.转义字符   Python 中的字符串还支持转义字符.所谓转义字符是指使用反斜杠“\”对一些特殊字符进行转义. \ 续行符 \n 换行符 \0 空 \t 水平制表符,用于横向跳到下一制表 ...

  2. php微信红包算法

    微信红包算法.php /**生成红包的函数*/ function getRandMoney($totalMoney, $totalPeople=2, $miniMoney=1){ $randRemai ...

  3. 使用VUE开发

    <一>VUE的开发分两种,一种是直接在HTML文件中使用,一种是VUE文件的形式开发 1,首先我们先让 HTML 文件支持 VUE 的语法指令提示 2,File -> Setting ...

  4. 【PHP】常用的PHP正则表达式收集整理

    匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xff]评注:可以用来计算字符串的长度 ...

  5. 解决Linux使用php命令 -base comment not found并安装composer

    获取php的安装目录 使用 find / -name php.ini 查看php的安装位置 /usr/local/php/lib/php.ini # cd 到/usr/local/php/lib/ph ...

  6. 下载速度更加快的 SourceForge 镜像

    http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/是 University of Kent的镜像, ...

  7. ZOJ Monthly, January 2018 训练部分解题报告

    A是水题,此处略去题解 B - PreSuffix ZOJ - 3995 (fail树+LCA) 给定多个字符串,每次询问查询两个字符串的一个后缀,该后缀必须是所有字符串中某个字符串的前缀,问该后缀最 ...

  8. Scrapy框架的命令行详解【转】

    Scrapy框架的命令行详解 请给作者点赞 --> 原文链接 这篇文章主要是对的scrapy命令行使用的一个介绍 创建爬虫项目 scrapy startproject 项目名例子如下: loca ...

  9. http--一次完整的HTTP事务是怎样一个过程?【转】

    一次完整的HTTP事务是怎样一个过程? 如有收获请给作者点赞 --> 原文链接 声明:本文章中的说法仅是个人理解总结,不一定完全正确,但是可以有助于理解. 当我们在浏览器的地址栏输入 www.l ...

  10. Kali 中文家目录改英文目录

    中文版Kali装好之后,家目录会中文显示,不便操作 root@kali:~# ls -l drwxr-xr-x root root .0K 7月 : 公共 drwxr-xr-x root root . ...