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. 错误号:1364 错误信息:Field 'platId' doesn't have a default value

    1. 错误描述 错误号:1364 错误信息:Field 'platId' doesn't have a default value insert into `use`.`t_platform_scal ...

  2. DML 触发器2

    2.行级触发器的关联标识符 :new,:old >>1. 一般通过:new.filed 引用(filed是trigger_table的字段名) :new :old中filed字段的意义 触 ...

  3. HashMap,LinkedHashMap,TreeMap对比

    共同点: HashMap,LinkedHashMap,TreeMap都属于Map:Map 主要用于存储键(key)值(value)对,根据键得到值,因此键不允许键重复,但允许值重复. 不同点: 1.H ...

  4. jquery绑定onkeyup()事件3中方法

    $('input').keyup(function () { ... }); $('input').bind('keyup', function () { ... }); $('input').liv ...

  5. PyTorch官方中文文档:torch.optim

    torch.optim torch.optim是一个实现了各种优化算法的库.大部分常用的方法得到支持,并且接口具备足够的通用性,使得未来能够集成更加复杂的方法. 如何使用optimizer 为了使用t ...

  6. 【CF235C】Cyclical Quest(后缀自动机)

    [CF235C]Cyclical Quest(后缀自动机) 题面 洛谷 题解 大致翻译: 给定一个串 然后若干组询问 每次也给定一个串 这个串可以旋转(就是把最后一位丢到最前面这样子) 问这个串以及其 ...

  7. Bzoj4805: 欧拉函数求和

    好久没写杜教筛了 练练手AC量刷起 # include <bits/stdc++.h> # define RG register # define IL inline # define F ...

  8. 魔改版ss-panel v3前端配置文件

    配置文件所在目录:网站根目录/config/.config.php <?php // ss-panel v3 配置 // // !!! 修改此key为随机字符串确保网站安全 !!! $Syste ...

  9. 设置mysql密码 Access denied 问题

    原文:http://www.upwqy.com/details/31.html 在Mac上安装完mysql以后 在终端执行 /usr/local/mysql/bin/mysql 可以直接进入.但是在设 ...

  10. El表达式的判断字符串的长度和截取,日期时间的格式化

    <c:if test="${fn:length(each.wii_name) >= 20}"> ${fn:substring(each.wii_name, 0,2 ...