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. 【memcache】windos下 memcache更改默认的端口和最大使用内存

    1>用内网ip的方式提供web应用服务器调用,不允许直接通过外网调用,如将memcache服务器放在192.168.1.55的服务器上 2>修改端口,如改为11200 3>分配内存, ...

  2. 如果没有Build path怎么办 .project文件的修改

    <?xml version="1.0" encoding="UTF-8"?><projectDescription> <name& ...

  3. GridView中使用 jQuery DatePicker (UpdatePanel)

    1.无UpdatePanel   1.代码 <script> $(function () { $('.myDatePickerClass').datepicker({ dateFormat ...

  4. jsp学习笔记之:内置对象

    application对象: 设置一个名为name,值为val的应用内共享的数据 <% application.setAttribute("name",val); %> ...

  5. 洛谷U19464 山村游历(Wander)(LCT,Splay)

    洛谷题目传送门 LCT维护子树信息常见套路详见我的总结 闲话 题目摘自WC模拟试题(by Philipsweng),原题目名Wander,"山村游历"是自己搞出来的中文名. 数据自 ...

  6. [ZOJ3435]Ideal Puzzle Bobble

    题面戳我 题意:你现在处于\((1,1,1)\),问可以看见多少个第一卦限的整点. 第一卦限:就是\((x,y,z)\)中\(x,y,z\)均为正 sol 首先L--,W--,H--,然后答案就变成了 ...

  7. Bzoj4199:[NOI2015]品酒大会

    题面 Bzoj4199 Sol 后缀数组 显然的暴力就是求\(LCP\)+差分 \(40\)分 # include <bits/stdc++.h> # define RG register ...

  8. c#多线程同步之Semaphore

    一提到Semaphore(信号量)的使用,还挺有意思的,它允许多个线程同时访问多个稀有资源,我立马想到银行的ATM机取钱的场景.看下面的代码: ); public static void StartT ...

  9. 用CSS画出好玩的图形

    1.上三角 #triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px so ...

  10. 解决将龙邱oled库移植到野火工程里,oled汉字无法显示问题

    第一,检查oled是否和单片机控制引脚正确相连. GND VCC CLK:时钟信号 miso RST: DC:DATE COMMAND/CONTROL CS:CHIP SELECT 第二,检查工程里是 ...