这道题就是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. 一步步理解linux字符设备驱动框架(转)

    /* *本文版权归于凌阳教育.如转载请注明 *原作者和原文链接 http://blog.csdn.net/edudriver/article/details/18354313* *特此说明并保留对其追 ...

  2. nyoj 20水

    #include<stdio.h> #include<string.h> #define N 110000 struct node { int u,v,next; }bian[ ...

  3. Maven错误-Missing artifact com.sun:tools:jar:1.5.0:system 解决方式

    1.Missing artifact com.sun:tools:jar:1.5.0:system Could not resolve dependencies for project com.ifl ...

  4. [ASP.NET]asp.net动态加载用户控件

    用户控件 // 用户控件源码 namespace wzjr.control { public partial class Topic : System.Web.UI.UserControl { pub ...

  5. Python Study (06)内存管理GC

    对象在内存的存储,我们可以求助于Python的内置函数id().它用于返回对象的身份(identity).其实,这里所谓的身份,就是该对象的内存地址. a = 1 print(id(a)) #1124 ...

  6. 初识cocos2dx——入门第一篇

    享受"啪"的按下Enter的快感. (by云风) cocos2dx是不是个toy引擎,不同的人有不同的看法.可是你不得不承认它非常受欢迎.好多非常火的手游使用的就是cocos2dx ...

  7. RecyclerView实现底部载入很多其它功能

    这两天在公司没有什么任务分配,就研究了下咱们Google在Android5.0 推出的一个用来取代ListView的列表控件----RecyclerView. 发现功能上确实比ListView强大了不 ...

  8. 英语发音规则---P字母

    英语发音规则---P字母 一.总结 一句话总结: 1.P发[p]音? paper ['peɪpə] n. 纸 plane [pleɪn] n. 飞机 pig [pɪg] n. 猪 ship [ʃɪp] ...

  9. 18.QT消息链筛选机制以及组合键

    mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> 5 #include <Q ...

  10. lightshot截图工具的安装及使用

    通常我们做PPT或者写博客难免要用到截图工具,而Windows自带的snippingtool启动有延迟也不够方便,QQ有截屏又需要联网及登录情况下,于是我想着在Chrome上搜一款清新简洁的截屏软件, ...