<状压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][ ...
随机推荐
- http、https、SSL、TLS的区别
一.HTTP和HTTPS之间的区别 HTTP是一种协议,全称叫作:超文本传输协议(HTTP,HyperText Transfer Protocol),是互联网上应用最为广泛的一种网络协议.所有的WWW ...
- 【uuid】- 唯一标识
2020-01-02 UUID ,Universally Unique Identifier ,通用唯一标识符. //定义一个生成 uuid 的方法const getUuid = () => { ...
- 利用shell脚本实现每隔60秒磁盘内存数据监控脚本
#!/bin/bash #Author:GaoHongYu #QQ: #Time:-- :: #Name:ncjk.sh #Version:V1. clear xtip=$(hostname -I) ...
- 【转】分布式服务框架 Zookeeper -- 管理分布式环境中的数据
Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理 ...
- 多态(C++)
#include <iostream> using namespace std; class HeroFighter { public: virtual int power() { ; } ...
- EntityFramework Core表名原理解析,让我来,揭开你神秘的面纱
前言 上一节我们针对最开始抛出的异常只是进行了浅尝辄止的解析,是不是有点意犹未尽的感觉,是的,我也有这种感觉,看到这里相信您和我会有一些疑惑,要是我们接下来通过注解.Fluent APi.DbSet分 ...
- 人群密度检测MCNN+CSRnet
MCNN(简单理解): 三列卷积神经网络,分别为大中小三种不同尺度的卷积核,表示为L列(使用大尺度卷积核: 9*9, 7*7, 7*7,7*7), M(使用中等尺度卷积核: 7*7, 5*5, 5*5 ...
- LR Java脚本编写方法
之前在某一家银行也接触过java写的性能接口脚本,最近因项目,也需编写java接口性能测试脚本,脑袋一下懵逼了,有点不知道从何入手.随后上网查了相关资料,自己又稍微总结了一下,与大家共同分享哈~ 首先 ...
- flask部署深度学习模型
flask部署深度学习模型 作为著名Python web框架之一的Flask,具有简单轻量.灵活.扩展丰富且上手难度低的特点,因此成为了机器学习和深度学习模型上线跑定时任务,提供API的首选框架. 众 ...
- 树dp 统计异或值
链接:https://ac.nowcoder.com/acm/contest/272/B来源:牛客网 题目描述 给定一棵n个点的树,每个点有权值.定义表示 到 的最短路径上,所有点的点权异或和. ...