Hie with the Pie poj-3311

    题目大意:n+1个点,伪旅行商问题。

    注释:n<=10。

      想法:咳咳,第一道状压dp,下面我来介绍一下状压dp。

        所谓dp,就是动态性决策规划,通过上一时刻或上几时刻的状态来更新当前状态并且无后效性。而状压dp就是将之前的状态通过二进制表现出来。几个例子。有五个格子_ _ _ _ _。上面可以放棋子或者不放。我们将放棋子的格子标注为1,不放棋子的格子标注为0。那么,我们就可以用一个二进制数来表达出人任何一个的完整状态而不是片面的,这就是状压dp。但是由于我们需要表示出所有的状态,所以状压dp的空间复杂度是指数级的,这就比较的伤心。所以看见题目的数据范围有那么一个小的可怜的,可以考虑考虑状压dp。状压dp前置知识点是位运算,在此不做介绍。

      关于这道题,我们设dp[s][i]。表示s这个状态的最小代价,且这个状态最后到达的点是i。之后转移就是枚举s的上一个到达的点。在此,我们要注意,题目中给出的是邻接矩阵的形式,我们先用floyd求出两点之间的最短路,之后通过上一个到达的点的状态加上dis[j][i]来更新当前状态。

    最后,附上丑陋的代码... ...

#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
int dp[5000][20];
int map[20][20];
int main()
{
int n;
while(1)
{
memset(map,0x3f,sizeof(map));
memset(dp,0,sizeof(dp));
scanf("%d",&n);
if(!n) return 0;
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
scanf("%d",&map[i][j]);
}
}
for(int k=0;k<=n;k++)//floyd
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
}
}
}
for(int S=0;S<=(1<<n)-1;S++)//枚举所有状态
{
for(int i=1;i<=n;i++)//考虑最后到达的点
{
if(S&(1<<(i-1)))//现判断i是不是s中已经到达的点
{
if(S==(1<<(i-1)))//特判,如果s只到达过i
{
dp[S][i]=map[0][i];
}
else
{
dp[S][i]=inf;
for(int j=1;j<=n;j++)
{
if(S&(1<<(j-1))&&(j!=i))
{
dp[S][i]=min(dp[S^(1<<(i-1))][j]+map[j][i],dp[S][i]);
}
}
}
}
}
}
int ans=inf;
for(int i=1;i<=n;i++)//最后所有的点全部都到达后,枚举最后到达的点去统计答案。
{
ans=min(ans,dp[(1<<n)-1][i]+map[i][0]);
}
printf("%d\n",ans);
}
}

  小结:题目中明确指出所给的邻接矩阵可能不是对称的,容易在floyd时将其按照对称处理。

Hie with the Pie的更多相关文章

  1. poj3311 Hie with the Pie (状态压缩dp,旅行商)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3160   Accepted: 1613 ...

  2. poj 3311 Hie with the Pie

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

  3. poj 3311 Hie with the Pie dp+状压

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 ...

  4. Hie with the Pie(poj3311)

    题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...

  5. Hie with the Pie POJ - 3311

    Hie with the Pie POJ - 3311 The Pizazz Pizzeria prides itself in delivering pizzas to its customers ...

  6. poj 3311 Hie with the Pie (TSP问题)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 ...

  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 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

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

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

随机推荐

  1. MySQL插入数据异常

    MySQL插入数据异常 1.错误如下: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Dupli ...

  2. 中断处理程序不能使用printf的本质

    vxworks 中断处理程序之所以不用printf,本质在于printf是将信息输出到标准输出设备(STDOUT)中, 整个标准输出设备是一个全局变量,由于有semTake操作,那么就会发生阻塞,vx ...

  3. freemarker写select组件(三)

    freemarker写select组件 1.宏定义 <#macro select id datas value="" key="" text=" ...

  4. SVN同步出现问题

    1.错误描述    同步SVNStatusSubscribe时报告了错误,1中的0个资源已经同步    同步/frame时发生错误:Error getting status for resource ...

  5. 获取JSON对象的属性名称

    1.问题背景 一个json对象,是以键值对组成,通过循环json对象,获取json对象中的属性名称 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  6. 如何让window.open()以post请求方式调用(巧妙解法)

    问题由来: 在公司遇到一个线上bug,如下 var url = 'http://106.75.31.215:8012/onlinePreview?url=' + encodeURIComponent( ...

  7. Codeforces Round #454 D. Seating of Students

    分三类 1 1: 一个就好了 3 3:特殊讨论下 或 : 第一行奇序号的数放前面,偶序号的数放后面,第二行奇序号的数放前面,偶序号的数放后面,第二行依次类推 有点难写,真的菜 #include< ...

  8. Java 第三章 选择结构

    第三章   选择结构 if基本语法: if(条件){// 表达式 // 代码块 } eg: int a = 10; if(a > 1){ System.out.println("内容& ...

  9. ASP.NET Core 2.0: 二. 开发环境

    macOS:Install Visual Studio for Mac 系统要求: macOS 10.12 Sierra 及更高版本 其他要求: 可能会要求安装xcode或android相关环境, 详 ...

  10. debug和release下PostThreadMessage的异同

    MFC中创建线程分为工作线程和UI线程.其中UI线程可以通过继承CWinThread进行创建. 创建函数如下: CWinThread *m_pRecogThread;//语音识别线程 m_pRecog ...