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),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...
随机推荐
- 科学计算库Numpy——文件读写
读文件 要读取的文件 有分隔符的文件 备注:delimiter分隔符. 有多余行的文件 备注:skiprows去掉几行. 指定列 备注:usecols指定使用哪几列. 写文件 保存后的文件 备注:fm ...
- Flask初学者:配置文件
如果设置项比较少的话可以使用“app.config['param_name']=value”的形式直接使用,如果需要设置的参数比较多的话,可以单独新建一个配置文件用来存放配置信息,配置文件中的参数需大 ...
- 问题 1936: [蓝桥杯][算法提高VIP]最大乘积
问题 1936: [蓝桥杯][算法提高VIP]最大乘积 时间限制: 1Sec 内存限制: 128MB 提交: 77 解决: 16 题目描述 对于n个数,从中取出m个数,如何取使得这m个数的乘积最大呢? ...
- linux命令行操作基本知识
乱七八糟的命令 . 表示当前目录 .. 表示上一级目录 ls 显示文件 -l 列表 -a 隐藏文件 -h 文件大小人性化显示 gedit 自带文本编辑器 subl 打开sublime > 重定向 ...
- Kinect安装
在连接kinect机器前,需要先安装两个软件,而在安装这两个软件前需要有vs2010(专业版本和快速版),因为需要包含.net framework 4.0 kinect sdk http://www. ...
- Android 自定义 radiobutton
<RadioButton android:id="@+id/radiobutton_pay_method" android:layout_width="30dp&q ...
- python3与django中@property详解
django提供了内置装饰器 @staticmethod\@classmethod\property 在OSQA中,@property的使用频率是非常高的.下面就是它的使用方法: @property ...
- leetcode 【 Reverse Words in a String 】python 实现
题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...
- MongoDB快速入门学习笔记4 MongoDB的文档查询操作
先把student删除,再重新插入数据 > db.student.drop() true > db.student.insert([{ "_id" : 1, " ...
- Memcached相关内容总结
1.Memcached常用命令总结 Memcached命令格式一般为: command 其中描述如下: 参数 描述 command 操作命令,一般为set/add/replace/get/delete ...