题意:tsp问题,经过图中所有的点并回到原点的最短距离. 解题关键:floyd+状态压缩dp,注意floyd时k必须在最外层 转移方程:$dp[S][i] = \min (dp[S \wedge (1 <  < (i - 1))][k] + dis[k][j],dp[S][i])$ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include&…
题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对于新手的我,看起来可能更方便些吧,顺便说下快捷键 先选中要操作的行,ctrl+shift+c 是注释 ctrl+shift+x是解注释(cb的快捷键) /* Floyd + 状态压缩DP 题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且距离最短 也就是TSP…
Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even if it mea…
题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:12225   Accepted: 6441 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortuna…
链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多少. 思路:首先用floyd找到全部点之间的最短路.然后用状态压缩,dp数组一定是二维的,假设是一维的话不能保证dp[i]->dp[j]一定是最短的.由于dp[i]记录的"当前位置"不一定是能使dp[j]最小的当前位置.所以dp[i][j]中,i表示的二进制下的当前已经经过的状态,j…
题目大意:从起点 1 开始走遍所有的点,回到起点 1 ,求出所走的最短长度. 思路:首先利用 Floyed 求出任意两点之间的最短距离 dis[i][j].求出任意两点之间的最短距离后,运用动态规划.dp[s][i] 表示当前状态为s时,最后一个到达的点为 1 时走过的最短距离. 将状态状态 s 看成一个二进制数,每一个二进制位表示一个点是否被访问,若第 i 位为1时表示第 i 个点被访问过了,为 0 则表示未访问. dp[ s | ( 1 << i )][ i ] = min( dp[s][…
描述 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 processed…
本题是经典的Tsp问题的变形,Tsp问题就是要求从起点出发经过每个节点一次再回到起点的距离最小值,本题的区别就是可以经过一个节点不止一次,那么先预处理出任意两点之间的最短距离就行了,因为再多走只会浪费更多的距离. dp[S][u]表示当前已访问的节点集合为S,从u出发走完剩余节点回到起点的最短距离. 边界条件:dp[(1<<n)-1][0]=0,最后的答案就是dp[0][0]: 记忆化递归代码: 1 #include<cstdio> 2 #include<cstring>…
题目链接:http://poj.org/problem?id=3311 思路:Floyd + 状态压缩DP  题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路,从0出发,又回到0,而且距离最短 (可重复走).首先不难想到用FLOYD先求出任意2点的距离dis[i][j]  .接着枚举所有状态,用11位二进制表示10个城市和pizza店,1表示经过,0表示没有经过  .定义状态DP(S,i)表示在S状态下,到达城市I的最优值  .接着状态转移方程:DP(S,i) = min{DP(…
题目链接: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…