链接: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. Nancy 框架

    Nancy 框架 Nancy 框架 1.是一个轻量级用于构建http相应的web框架: 2.与mvc类似,有自己的路由机制: 3.可以处理 DELETE ,  GET ,  HEAD ,  OPTIO ...

  2. 搜索引擎排名不友好的五个地点-SEO

    搜索引擎(百度/谷歌/雅虎)排名不友好的五个地点 别的站点,推断标准和考核得分点是不 一样的,避免对百度排名不友好的五种站点操作 你的站点是否在这五种站点里,决定你的站点能否获得排 名. 1.有没有同 ...

  3. 它们的定义actionbar 并删除留空

    通过他们自己的定义actionbar布局变化actionbar样式,简单而美丽.但有一个细节需要注意的是,高分辨率的问题留空.一般720上述决议,下一次你发现,无论什么样的变化总是会有一个小的布局文件 ...

  4. HDU 4819 Mosaic D区段树

    连接:pid=4819">http://acm.hdu.edu.cn/showproblem.php?pid=4819 意:给出一个800×800下面的矩阵.每次更新一个点的值为以这个 ...

  5. Android供TextView添加多个点击文字

    我们使用社会性软件的过程中会或多或少像别人的帖子点,图. : 能够看到用户页面显示出来的仅仅是点了赞的用户的名称,点击这些名称能够进入到该用户的主页.我们就来实现相似的效果.直接上代码吧. @Over ...

  6. DOM简要

    在看Js视频的时候就感觉Dom这东西太奇妙了.在这个注重用户体验的Web设计时代里.Dom是至关重要的. 它的易用性强.而且遍历简单.支持XPath. 它既然这么强大那么就来简单的介绍Dom这个东东. ...

  7. git-push(1) Manual Page

    git-push(1) Manual Page NAME git-push - Update remote refs along with associated objects SYNOPSIS gi ...

  8. 解决IE下Ajax请求无效

    在做web开发是,大多时候都会使用FireFox作为调试的浏览器.上面携带的FireBug用来调试JavaScript实在是太方便了,绝大多数的问题都能够通过它跟踪调试出来.但是,当项目发布时,不能仅 ...

  9. js控制文本框仅仅能输入中文、英文、数字与指定特殊符号

    JS 控制文本框仅仅能输入数字 <input onkeyup="value=value.replace(/[^0-9]/g,'')"onpaste="value=v ...

  10. Theano学习笔记(三)——图结构

    图结构(Graph Structures)这是理解Theano该基金会的内部运作. Theano编程的核心是用符号占位符把数学关系表示出来. 图结构的组成部分 如图实现了这段代码: importthe ...