POJ--3311--Hie with the Pie(暴力枚举+floyd)/(状态压缩DP+floyd)

简介

状态压缩入门,先说用暴力枚举的方法做的,再说状态压缩DP,对于刚开始学习状态压缩的同学,两者相互比较学习,可以明显看出两者区别,有利于对状态压缩DP的理解,提前说下,两者耗时是 157Ms和 0Ms 。

题意

一披萨店员送披萨,从披萨店开始送给N个客户会给你一个(N+1)*(N+1)的矩阵,对于矩阵 g[i][j] 表示 i 处到 j 处所耗费的时间,0 代表披萨店,每条路径可以重复走。 问:店员从披萨店开始送完所有的披萨再返回店中的最短耗时。注意,出店就拿有 N 个披萨,不必重复返回店里拿;此矩阵不对称,即 g[i][j]!=g[j][i] 。

暴力枚举+floyd的思路:

如果枚举所路过的所有点,因为可以重复走每条线路,所以无法算出深度。应当换个思路再暴力,虽然无法枚举完下一个经过哪个点,但可以枚举完下一个经过哪个没有走过的点即1~N 的全排列,利用 floyd 预处理,算出 从 i 到 j 的最短路存入 g[i][j] ,这样可以忽视 i 到 j 中间路过哪些点;

例如,以此送货的路径是 0 1 2 3 4 5 0 (N=5) ,该路径耗时就是 g[0][1]+g[1][2]+g[2][3]+g[3][4]+g[4][5]+g[5][0] ;

原文地址:https://www.cnblogs.com/l1l1/p/8570955.html

时间: 2024-07-29 03:48:31

POJ--3311--Hie with the Pie(暴力枚举+floyd)/(状态压缩DP+floyd)的相关文章

[POJ 3311]Hie with the Pie——再谈TSP问题的DP解法

题目连接:  http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 思路:用二进制数来表示访问过的城市集合,f[{S}][j]=已经访问过的城市集合为S,访问了j个城市,所需的最少花费. 这里提一下二进制数表示集合的方法(这里不妨设集合中最多有n个元素): 如果集合S中最多会出现n个元素,则用长度为n的二进制数来表示集合S,每一位代表一个元素,该位为0表示该元素在集合S中不存在,为1表示该元素在集

POJ 3311 Hie with the Pie(TSP问题 状压DP)

Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be

poj 3311 Hie with the Pie 【旅行商+回原点】

题目:poj 3311 Hie with the Pie 题意:就是批萨点小二要送批萨,然后给你每个点的距离,有向的,然后让你就走一次回到原点的最短路. 分析:因为给出的是稠密图,所以要处理一下最短路,floyd 然后TSP就好. 枚举每个状态,对于当前状态的每一个已经走过的点,枚举是从那个点走过来的,更新最短路 状态:dp[st][i] :st状态下走到点 i 的最短路 转移方程:dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]);

POJ 3311 Hie with the Pie (状压DP)

状态压缩DP dp[i][j]表示在i状态(用二进制表示城市有没有经过)时最后到达j城市的最小时间 转移方程dp[i][j]=min(dp[i][k]+d[k][j],dp[i][j]) d[k][j]是k城市到j城市的最短距离 要先用flody处理 #include<bits.stdc++.h> using namespace std; int d[20][20],dp[1<<11][20]; int n,m; void flody() { for(int k=0;k<=n

POJ 3311 Hie with the Pie TSP+Floyd

保证每个点访问过一次就行,然后会到原点. 这种情况可以先做一边floyd,然后跑tsp就好. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque&g

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

题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait fo

POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

贴一个TSP讲解:点击打开链接 错误的转移方程 dp[i][j] 把i当作了步数,以为至多走N步就可以了.作死啊 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #define maxn 1100 #define inf 0x3f3f3f3f const double eps=1e-8; using namespace std; int dp[12][1<

POJ 2288 Islands and Bridges 哈密尔顿路 状态压缩DP

找最长的其实是很裸的状态压缩DP,棘手的地方是要统计数量,其实只要再来一个数组存就好. 不过代码比较长,细节要注意的地方毕较多,wa了很多发,还是要仔细啊 用递推和记忆化搜索分别写了一遍 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <

poj 3311 Hie with the Pie(状态压缩dp)

Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be