<状压DP>solution-POJ3311_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的更多相关文章
- POJ3311 Hie with the Pie 【状压dp/TSP问题】
题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total ...
- POJ 3311 Hie with the Pie (状压DP)
dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...
- 状压dp+floyed(C - Hie with the Pie POJ - 3311 )
题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...
- Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)
题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...
- poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp
题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间 ...
- POJ 3311 Hie with the Pie (状压DP)
题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...
- 【POJ3311】Hie with the Pie(状压DP,最短路)
题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...
- 【BZOJ-1097】旅游景点atr SPFA + 状压DP
1097: [POI2007]旅游景点atr Time Limit: 30 Sec Memory Limit: 357 MBSubmit: 1531 Solved: 352[Submit][Sta ...
- BZOJ 1087 题解【状压DP】
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3112 Solved: 1816[Submit][ ...
随机推荐
- Kerrigan:配置中心管理UI的实现思路和技术细节
去年写过一篇文章『中小团队落地配置中心详解』,介绍了我们借助etcd+confd实现的配置中心方案,这是一个对运维友好,与开发解耦的极佳方案,经过了一年多的实践也确实帮我们解决了配置文件无版本.难回滚 ...
- IteratorPattern(迭代器模式)-----Java/.Net
迭代器模式(Iterator Pattern)是 Java 和 .Net 编程环境中非常常用的设计模式.这种模式用于顺序访问集合对象的元素,不需要知道集合对象的底层表示
- 【汇编】AX内容依次倒排序
;P99,5.13,ax内容倒序 ;思路,ax左移一位最高位进cf里,bx右移一位把cf里值进bx的最高位, ;循环16次即实现ax16位内容倒序存储在bx中 DATA SEGMENT DATA EN ...
- bat无法识别命令、无法识别运算符、结果不符合预期等问题
1.无法识别的命令等可能为中文字符编码等问题,破坏了bat文件格式,建议去掉中文,加上引号或者重新创建文件.例如:@echo off%~d0cd /d %~dp0title epoch时间转换(%cd ...
- k8s(1.14.0)+etcd(3.3.10)+flanneld(0.10)
K8s(1.14) 几张比较不错的图 1.kubernetes 组件图 kubernetes 架构图 2.kubernetes 网络架构图 数据从源容器中发出后,经由所在主机的docker0虚拟网卡转 ...
- 鼠标右键新建Markdown文档
首先放一张github某项目中.md文件中的内容图片 Windows系统下,使用 Typora 软件来进入Markdown文档的编写非常容易,而且入门门槛非常的低 存在的问题: 习惯了使用Markdo ...
- Session是怎么实现的?存储在哪里?
为什么有session? 首先大家知道,http协议是无状态的,即你连续访问某个网页100次和访问1次对服务器来说是没有区别对待的,因为它记不住你. 那么,在一些场合,确实需要服务器记住当前用户怎么办 ...
- STM321的SPI驱动遇到的一个坑!!
最近在做项目要用到FATFS文件驱动和SD卡驱动,SD卡驱动我用的是SPI的通信方式,在挂载文件系统是总是挂在失败了,花了一天时间反复检查,才发现SPI在接收时候卡死: 为了寻找问题的原因,整个人都快 ...
- STM32串口遇到的一个问题
做HLW8032电能表项目中关于USART使用DMA接收定长数据的问题 1:由于HLW8032芯片一上电,芯片就会通过串口每隔50ms向STM32发送24字节的数据,且我不能通过STM32控制HLW8 ...
- 什么是aPaas?aPaas与低代码又是如何促进应用程序开发现代化的?
从软件即服务(SaaS)到基础设施即服务(IaaS),云计算的兴起使“一切皆服务”(XaaS)模型得以泛滥,而aPaaS可能是这些模型中最鲜为人知的模型.随着aPaaS市场预计将从2018年的近90亿 ...