这道题就是Tsp问题,稍微加了些改变

注意以下问题

(1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离

(2)在循环中集合可以用S表示更清晰一些

(3)第一维为状态,第二维为在哪个点,不要写混。

(4)在dp过程中0这个点是不用的,只用到1到n这个点

而实际上dp过程中用的是0到n-1,所以就枚举1到n,然后涉及到集合的地方就写i-1

其他地方如dp的第二维,距离这些都不变。

还有一个方法,是我一开始想的方法,就是在输入的时候就把0放到第n个点,其他下标-1

(4)这道题求最小值,一定要先初始化为最大值

方法一(涉及到集合的时候下标-1)

#include<cstdio>
#include<cstring>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std; const int MAXN = 15;
int dist[MAXN][MAXN], dp[1 << 11][MAXN], n; int main()
{
while(~scanf("%d", &n) && n)
{
_for(i, 0, n)
_for(j, 0, n)
scanf("%d", &dist[i][j]); _for(k, 0, n)
_for(i, 0, n)
_for(j, 0, n)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); memset(dp, 0x3f, sizeof(dp));
_for(i, 1, n) dp[1 << (i - 1)][i] = dist[0][i];
REP(S, 0, 1 << n)
_for(i, 1, n) if(S & (1 << (i - 1)))
_for(j, 1, n) if(S & (1 << (j - 1)))
dp[S][i] = min(dp[S][i], dp[S^(1<<(i-1))][j] + dist[j][i]); int ans = 1e9;
_for(i, 1, n)
ans = min(ans, dp[(1 << n) - 1][i] + dist[i][0]);
printf("%d\n", ans);
} return 0;
}

方法二(直接在输入的时候改变)

#include<cstdio>
#include<cstring>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std; const int MAXN = 15;
int dist[MAXN][MAXN], dp[1 << 11][MAXN], n; int main()
{
while(~scanf("%d", &n) && n)
{
_for(i, 0, n)
_for(j, 0, n)
scanf("%d", &dist[(!i ? n : i - 1)][(!j ? n : j - 1)]); _for(k, 0, n)
_for(i, 0, n)
_for(j, 0, n)
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); memset(dp, 0x3f, sizeof(dp));
REP(i, 0, n) dp[1 << i][i] = dist[n][i];
REP(i, 0, 1 << n)
REP(j, 0, n) if(i & (1 << j))
REP(k, 0, n) if(i & (1 << k))
if(j != k)
dp[i][j] = min(dp[i][j], dp[i^(1<<j)][k] + dist[k][j]); int ans = 1e9;
REP(i, 0, n)
ans = min(ans, dp[(1 << n) - 1][i] + dist[i][n]);
printf("%d\n", ans);
} return 0;
}

poj 3311 Hie with the Pie (状压dp) (Tsp问题)的更多相关文章

  1. POJ 3311 Hie with the Pie (状压DP)

    dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...

  2. 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]

    题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...

  3. East Central North America 2006 Hie with the Pie /// 状压dp oj22470

    题目大意: 输入n,有n个地方(1~n)需要送pizza pizza点为0点 接下来n+1行每行n+1个值 表示 i 到 j 的路径长度 输出从0点到各点送pizza最后回到0点的最短路(点可重复走) ...

  4. poj 2288 Islands and Bridges (状压dp+Tsp问题)

    这道题千辛万苦啊! 这道题要涉及到当前点和前面两个点,那就设dp[state][i][j]为当前状态为state,当前点为i,前一个点为j 这个状态表示和之前做炮兵那题很像,就是涉及到三个点时,就多设 ...

  5. poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp

    题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间 ...

  6. POJ 3311 Hie with the Pie (状压DP)

    题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...

  7. POJ3311 Hie with the Pie 【状压dp/TSP问题】

    题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total ...

  8. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  9. Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)

    题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...

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

    题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...

随机推荐

  1. 交互式编程之Golang基本配置(Jupyter-notebooks Golang)

    JupyterNoteBook-GO 启动错误 Install Go Install gophernotes 参考资料 如有错误,欢迎指出 错误 error: Cannot assign reques ...

  2. 00064_字符串缓冲区_StringBuffer类

    1.StringBuffer类 (1)StringBuffer又称为可变字符序列,它是一个类似于 String 的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容. (2)tringBuffe ...

  3. hdu 1556 线段树区间延迟更新好题

    656mS #include<stdio.h> #include<stdlib.h> #define N 110000 struct node { int x,y,yanchi ...

  4. 浙大PAT考试1013~1016(最伤的一次。。)

    我能说我1016WA了几天都不得最后还是拿别人代码交的么. .. 真心找不到那个神数据.. . 自己把整个程序的流程都画出来了.细致推敲是木有问题的啊... 题目地址:点击打开链接 先从1013開始介 ...

  5. [HTML 5] Styling with ARIA

    See if you can do a better job styling this button using ARIA states. One huge benefit to styling wi ...

  6. getColor()方法过时的替代方法

    Android SDK 升級到 23 之後,getResource.getColor(R.color.color_name) 過時 使用新加入的方法 ContextCompat.getColor(co ...

  7. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  8. JS 正则表达式的位置匹配ZZ

    http://regexpal.com/ 上面这个网站可以用于在线检测JS的正则表达式语法 除了熟知的几个固定字符表示位置: ^ : Match the beginning of the string ...

  9. 门面模式(Facade)

    一:定义 提供一个统一的接口代表子系统内部的一组接口.门面模式提供一个高层的接口,使得子系统更易于使用.   二:经验 2.1 window系统的软关机(不是直接断电)是一个过程, 它自己背后会做很多 ...

  10. Anaconda安装及PyCharm环境配置

    1. Anaconda下载 Anaconda 官方下载链接: https://www.continuum.io/downloads 根据自己的系统选择下载32位还是64位. 2. 进入下载目录 如果没 ...