题目链接

题意

给定一个\(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. centos7上基于kubernetes的docker集群管理

    kubernetes和docker的作用这里就不作介绍了,直接进入主题. 本文的目的是搭建docker集群,并使用kubernetes管理它们. 文中的软件环境除了kubernetes和docker, ...

  2. 使用paramiko报错:CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_poi

    1.paramiko不支持cryptography-2.6.1 pip3 uninstall cryptography==2.6.1 2.paramiko 支持cryptography-2.4.2 p ...

  3. 使用IP访问本地网站缓慢解决方法

    运行环境: win7 64位 apache2.4 php-5.6.37 最近由于业务需要,需要使用php5.6版本,所以配套升级了apache到2.4,但升级以后,发现用公司内网IP访问网站非常非常慢 ...

  4. jQuery编码中的一些技巧

    缓存变量 DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // ...

  5. django之配置静态文件

    # 别名 STATIC_URL = '/static/' # 配置静态文件,名字必须是STATICFILES_DIRS STATICFILES_DIRS = [ os.path.join(BASE_D ...

  6. VS2010官方下载地址

    http://download.microsoft.com/download/2/4/7/24733615-AA11-42E9-8883-E28CDCA88ED5/X16-42552VS2010Ult ...

  7. hdu 1257最少拦截系统

    最少拦截系统 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的 ...

  8. 笔记-python-selenium,phantomjs

    笔记-python-selenium,phantomjs 1.      简介 1.1.    selenium selenium是一款自动化测试工具,支持多种语言 为什么爬虫要使用selenium呢 ...

  9. 1022: [SHOI2008]小约翰的游戏John9(Auti_SG)

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3150  Solved: 2013[Submit] ...

  10. cf984e Elevator

    ref我好菜啊 #include <iostream> #include <cstring> #include <cstdio> #include <cmat ...