poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp
题目链接
题意
给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点)。
思路
因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间的最短路径。
接下来就是状压dp的部分。
将已经经过的点的状态用\(state\)表示,
则\(dp[state][k]\)表示当前到达点\(k\)后状态为\(state\)时的最短路径长度。
\]
可用记忆化搜索。
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的更多相关文章
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)
题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...
- POJ 3311 Hie with the Pie(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...
- poj 3311 Hie with the Pie dp+状压
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4671 Accepted: 2471 ...
- poj 3311 Hie with the Pie (TSP问题)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4491 Accepted: 2376 ...
- POJ 3311 Hie with the Pie 最短路+状压DP
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11243 Accepted: 5963 ...
- POJ 3311 Hie with the Pie(DP状态压缩+最短路径)
题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...
- [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
主题连接: id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...
- POJ 3311 Hie with the Pie floyd+状压DP
链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...
随机推荐
- Python3爬取人人网(校内网)个人照片及朋友照片,并一键下载到本地~~~附源代码
题记: 11月14日早晨8点,人人网发布公告,宣布人人公司将人人网社交平台业务相关资产以2000万美元的现金加4000万美元的股票对价出售予北京多牛传媒,自此,人人公司将专注于境内的二手车业务和在美国 ...
- Python9-From-CSS-day48
1.form表单相关内容前后端有数据交互的时候用form表单form表单提交数据的几个注意事项: 1.所有获取用户输入的标签都必须放在form表单里面 2.action 控制着往哪里提交 3.inpu ...
- Python9-MySQL-MySQL存储过程-视图-触发器-函数-day45
视图:某个查询语句设置别名,日后方便使用 CREATE VIEW v1 as SELECT * FROM student WHERE sid >10 -创建: create view 视图名称 ...
- Paul Zindel【保罗·金代尔】
Paul Zindel Paul Zindel's death on March 27, 2003 ended the brilliant life of a famous write. 2003年3 ...
- Diycode开源项目 Glide图片加载分析
1.使用Glide前的准备 1.1.首先要build.gradle中添加 github原地址点击我. 参考博客:Glide-开始! 参考博客:android图片加载库Glide的使用介绍. 参考博 ...
- WinRM和WinRS
1.6.2 新远程工具:WinRM和WinRS 2012-05-14 10:18 张杰良 译 清华大学出版社 字号:T | T 综合评级: 想读(18) 在读(5) 已读(0) 品书斋鉴(0) ...
- 设计模式之第10章-桥接模式(Java实现)
设计模式之第10章-桥接模式(Java实现) “一入软件深似海,从此早睡是路人.黑夜给了我黑色的眼睛,我却用他去寻找八阿哥.”“怎么了,又来那么多的感慨啊.”“还能有什么啊,老板是说让换个APP做,这 ...
- Ant 概念
Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令其实 ...
- Codeforces Round #418 (Div. 2) A+B+C!
终判才知道自己失了智.本场据说是chinese专场,可是请允许我吐槽一下题意! A. An abandoned sentiment from past shabi贪心手残for循环边界写错了竟然还过了 ...
- 【bzoj1316】树上的询问 树的点分治+STL-set
题目描述 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 输入 第一行两个整数n, p分别表示点的个数和询问的个数. 接下来n-1行每行 ...