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),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...
随机推荐
- 问题:Could not install packages due to an EnvironmentError: [Errno 13] Permission denied:
1.安装django 执行pip3 install --user django 2.解决方法:加--user 执行pip3 install --user django
- JZOJ 4743. 积木
Description Input Output Sample Input 38 7 63 9 41 10 5 Sample Output 18
- python正则表达式入门篇
文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...
- 20181225 基于TCP/IP和基于UDP/IP的套接字编程
一.TCP/IP的套接字编程 服务器端代码: import socketserver = socket.socket() # 默认是基于TCP# 基于TCP的对象serve=socket.sock ...
- (转)iOS静态库与动态库的区别
一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用 ...
- kuangbin 并查集
A : Wireless Network POJ - 2236 题意:并查集,可以有查询和修复操作 题解:并查集 #include<iostream> #include<cstdi ...
- Unity脚本执行顺序自研框架
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/52372611 作者:car ...
- Maven使用入门
Maven使用POM文件管理项目资源,pom.xml文件位于项目根目录下,结构如下: <?xml version="1.0" encoding="UTF-8&quo ...
- AD管理中心
(一).安装 Active Directory 管理中心 引用位置: http://technet.microsoft.com/zh-cn/library/dd560652(WS.10).aspx ( ...
- MongoDB快速入门学习笔记3 MongoDB的文档插入操作
1.文档的数据存储格式为BSON,类似于JSON.MongoDB插入数据时会检验数据中是否有“_id”,如果没有会自动生成.shell操作有insert和save两种方法.当插入一条数据有“_id”值 ...