链接:http://poj.org/problem?id=3311

题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多少。

思路:首先用floyd找到全部点之间的最短路。然后用状态压缩,dp数组一定是二维的,假设是一维的话不能保证dp[i]->dp[j]一定是最短的。由于dp[i]记录的“当前位置”不一定是能使dp[j]最小的当前位置。所以dp[i][j]中,i表示的二进制下的当前已经经过的状态,j表示的是在当前状态下眼下所在的位置。

代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define PI acos(-1.0)
#define seed 31//131,1313
#define maxn 15
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int dp[1<<10][maxn];
int Pow[maxn];
int back[maxn];
int cost[maxn][maxn];
void init1()
{
Pow[0]=1;
for(int i=1; i<=10; i++)
Pow[i]=Pow[i-1]*2;
}
void init2()
{
for(int i=1; i<(1<<10); i++)
for(int j=0; j<=10; j++)
dp[i][j]=INF;
}
int floyd(int a[][maxn],int t)
{
for(int i=0; i<t; i++)
for(int j=0; j<t; j++)
for(int k=0; k<t; k++)
a[i][j]=min(a[i][k]+a[k][j],a[i][j]);
}
int main()
{
int T,x;
init1();
while(scanf("%d",&T))
{
init2();
if(!T)
break;
for(int i=0; i<T+1; i++)
for(int j=0; j<T+1; j++)
scanf("%d",&cost[i][j]);
floyd(cost,T+1); for(int i=0; i<T; i++)
dp[Pow[i]][i]=cost[0][i+1];
for(int i=0; i<T; i++)
back[i]=cost[i+1][0];
for(int i=0; i<T; i++)
for(int j=0; j<T; j++)
cost[i][j]=cost[i+1][j+1];
for(int i=0; i<(1<<T); i++)
{
if(i==1||i==2||i==4||i==8||i==16||i==32||i==64||i==128||i==256||i==512)
continue;
int ii=i;
int pos=0;
while(ii)
{
if(ii%2==1)
{
int t=i-Pow[pos];
for(int j=0; j<T; j++)
if(dp[t][j]!=INF&&dp[t][j]+cost[j][pos]<dp[i][pos])
dp[i][pos]=dp[t][j]+cost[j][pos];
}
ii>>=1;
pos++;
}
}
int ans=INF;
for(int i=0; i<T; i++)
{
if(dp[(1<<T)-1][i]!=INF&&dp[(1<<T)-1][i]+back[i]<ans)
ans=dp[(1<<T)-1][i]+back[i];
} printf("%d\n",ans);
}
return 0;
}

POJ 3311 Hie with the Pie floyd+状压DP的更多相关文章

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

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

  2. POJ 3311 Hie with the Pie(状压DP + Floyd)

    题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...

  3. POJ 3311 Hie with the Pie 【状压DP】

    Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possi ...

  4. poj 3311 Hie with the Pie (状压dp) (Tsp问题)

    这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...

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

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

  6. TZOJ 1937 Hie with the Pie(floyd+状压dp)

    描述 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...

  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. 【POJ3311】Hie with the Pie(状压DP,最短路)

    题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...

  9. [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法

    主题连接:  id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...

随机推荐

  1. vs2015基于VisualStudioOnline协同工作流程

    项目负责人登陆自己的vsonline新建项目就不多说了. 直接从邀请队友开始 项目负责人操作 被邀请的邮箱务必是可以登录visualstudio的邮箱 发送邀请后,被邀请人登陆自己的邮箱,查看邀请人发 ...

  2. 介绍一款替代SSMS的sqlserver管理工具 toad for sqlserver5.7

    原文:介绍一款替代SSMS的sqlserver管理工具 toad for sqlserver5.7 toad for sqlserver5.7 虽然SSMS很好很强大,不过有时候使用一些第三方工具可以 ...

  3. JAVA设计模式--辛格尔顿

    Singleton模式可以作为一种编程技术,让我们先从理论上说代码 单例模式三个关键点: 1).某个类仅仅能有一个实例 2).该类必须自行创建这个实例 3).该类必须自行向整个系统提供这个实例 应用场 ...

  4. NET版微信客户端.

    微信客户端.NET版 目录 说明 功能 原理步骤 一些参考 说明 前两天比较闲,研究了一下web版微信.因为之前看过一篇博客讲微信web协议的,后来尝试分析了一下,半途中发现其实没什么意义,但又不想半 ...

  5. nginx 1.4.2 安装笔记

    顺便吐槽一下,有道笔记的排版太难用了, 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 ========================================== ...

  6. Android 实现蘑菇街购物车动画效果

    版本号:1.0  日期:2014.8.6 版权:© 2014 kince 转载注明出处   使用过蘑菇街的用户基本上都知道有一个增加购物车的动画效果,此处不详细描写叙述想知道的能够去下载体验一下. 1 ...

  7. ACM字符串处理算法经典:字符串搜索

    语法:result=strfind(char str[],char key[]); 参数: str[]:在这个源字符串查找操作 key[]:搜索字符串.不能为空字符串 回报值:     假设查找成功. ...

  8. vs2015web工程中的html引用压缩后css后无法智能提示的问题解决

    环境:win10x64 vs2015企业版 项目:空白web项目(.net framework4) 问题:html页面加入压缩后的css(eg:bootstrap.min.css),编码的时候无法智能 ...

  9. css3 menu 手机菜单1

    首先看一下效果图; 效果1,主要是 translateY(100px) -->translateY(0px);opacity:0;—>opacity: 1; 然后递归延迟 怕麻烦也可以自己 ...

  10. iOS一个开发系列中 - UIButton 使用摘要

    // 初始化button并设置类型 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 可以定义的UIButto ...