Hie with the Pie

Description

The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be processed before he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even if it means passing the same location(s) or the pizzeria more than once on the way. He has commissioned you to write a program to help him.

Input

Input will consist of multiple test cases. The first line will contain a single integer n indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will be n + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and the n locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to location j without visiting any other locations along the way. Note that there may be quicker ways to go from i to j via other locations, due to different speed limits, traffic lights, etc. Also, the time values may not be symmetric, i.e., the time to go directly from location i to j may not be the same as the time to go directly from location j to i. An input value of n = 0 will terminate input.

Output

For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return to the pizzeria.

Sample Input

3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0

Sample Output

8

人话:输入一个数n,现在有n个地方(标号1到n)要从标号为0的地方出去,经过所有的地方之后回来,求最短的时间,输入(n+1)*(n+1)的矩阵表示每两点之间到达所需要的时间

首先,先把每两个点之间的最短路求出来,使用floyd搞定

然后开始状压dp

我们把当前去过哪些点进行状压,i二进制表示从左到右第k位表示第k个点是否访问过

那么我们就可以把dp数组搞出来了,\(f[i][j]\)表示在已访问i状态这么多点的情况下,重点是j的最短路

状态转移方程就是:

\(f[i|(1<<k)][k] = f[i][j]+dis[j][k]\)

还有,起始点状态记得初始化

Code:

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <iostream>
#define reg register
using namespace std;
const int MaxN=11;
const int inf=0x3f3f3f3f;
template <class t> inline void rd(t &s)
{
s=0;
reg char c=getchar();
while(!isdigit(c))
c=getchar();
while(isdigit(c))
s=(s<<3)+(s<<1)+(c^48),c=getchar();
return;
}
int n;
int f[(1<<MaxN)+1][MaxN];
int G[MaxN][MaxN],dis[MaxN][MaxN];
inline void work()
{
memset(f,0x3f,sizeof f);f[1][0]=0;
for(int i=0;i<=n;++i)
for(int j=0;j<=n;++j)
rd(G[i][j]),dis[i][j]=G[i][j];
for(int k=0;k<=n;++k)
for(int i=0;i<=n;++i) if(k!=i)
for(int j=0;j<=n;++j) if(i!=j)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
for(int i=1;i<(1<<(n+1));++i)
{
for(int j=0;j<=n;++j) if(f[i][j]!=inf)
for(int k=0;k<=n;++k)
if(j!=k)
f[i|(1<<k)][k]=min(f[i|(1<<k)][k],f[i][j]+dis[j][k]);
}
reg int u=(1<<(n+1))-1;
printf("%d\n",f[u][0]);
return;
}
signed main(void)
{
while(cin>>n&&n)
work();
return 0;
}

<状压DP>solution-POJ3311_Hie with the Pie的更多相关文章

  1. POJ3311 Hie with the Pie 【状压dp/TSP问题】

    题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total ...

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

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

  3. 状压dp+floyed(C - Hie with the Pie POJ - 3311 )

    题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...

  4. Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)

    题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...

  5. poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp

    题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间 ...

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

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

  7. 【POJ3311】Hie with the Pie(状压DP,最短路)

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

  8. 【BZOJ-1097】旅游景点atr SPFA + 状压DP

    1097: [POI2007]旅游景点atr Time Limit: 30 Sec  Memory Limit: 357 MBSubmit: 1531  Solved: 352[Submit][Sta ...

  9. BZOJ 1087 题解【状压DP】

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3112  Solved: 1816[Submit][ ...

随机推荐

  1. 前端——jQuery介绍

    目录 jQuery介绍 jQuery的优势 jQuery内容: jQuery版本 jQuery对象 jQuery基础语法 查找标签 基本选择器 层级选择器: 基本筛选器: 属性选择器: 表单筛选器: ...

  2. 十三、springboot 优雅集成spring-boot-admin 实现程序监控

    前言 我们知道项目的监控是尤为重要的,但是我们如果用jdk 自带的jconsole 和jvisualvm 的话会非常繁琐,且界面不是很友好.之前我们使用了spring boot 项目,但是都没有对项目 ...

  3. 机器学习——EM算法与GMM算法

    目录 最大似然估计 K-means算法 EM算法 GMM算法(实际是高斯混合聚类) 中心思想:①极大似然估计 ②θ=f(θold) 此算法非常老,几乎不会问到,但思想很重要. EM的原理推导还是蛮复杂 ...

  4. MakeDown效果

    这是一级标题 这是二级标题 这是三级标题 这是四级标题 这是五级标题 这是六级标题 这是加粗的文字 这是倾斜的文字 这是斜体加粗的文字 这是加删除线的文字 这是引用的内容 这是引用的内容 这是引用的内 ...

  5. Spring工程报错

    错误日志: 2014-09-24 10:50:16 [org.springframework.context.support.FileSystemXmlApplicationContext]-[INF ...

  6. 悄摸直播(三)—— 搭建rtmp服务器(smart_rtmpd - rtmp服务器搭建)

    悄摸直播 -- javaCV实现本机摄像头画面远程直播 搭建rtmp服务器 一.素材 rtmp服务器:smart_rtmpd ffmpeg工具:ffmpeg.exe 二.搭建 1.下载smart_rt ...

  7. 1z0-062 题库解析1

    You configured the Fast Recovery Area (FRA) for your database. The database instance is in archivelo ...

  8. Android 平台JS调试技术

    1.  测试技术简介 Android平台微信公众号一般以H5的形式开发,测试发现流量一般都通过js进行加密传输,导致无法对越权.SQL注入等风险点进行测试.针对此难点,本手册会介绍包括Android环 ...

  9. Ambari下安装oozieUI界面无法访问问题

    前言: 其他集群管理工具安装oozie和手动编译安装oozie,关于oozie的UI界面无法访问的问题,大致和该方法类似. 找到下面4步骤里的视图显示的目录,有一个ext-2.2软链指向的路径 lrw ...

  10. Openstack之七:实现基于桥接的内外网络

    一.在控制端进行配置网络 #启动实例文档:https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/launch-instance.html# ...