Hie with the Pie

  • 题目
  • 解题思路
  • Code

POJ P3311 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≤10用Floyd就可以了
设 f[i][j] ,i 为所有点选没选(1/0)的状态,j 为最后走的一个点
转移就是很传统的状态转移,详见程序


Code

#include <iostream>
#include <cstring>
#include <cstdio>using namespace std;const int Inf = 0x7f7f7f7f;
int n, ans, a[30][30], f[1 << 15][30];int main() {scanf("%d", &n);while(n) {memset(a, 0x3f, sizeof(a));for(int i = 0; i <= n; i ++)for(int j = 0; j <= n; j ++)scanf("%d", &a[i][j]);for(int k = 0; k <= n; k ++) for(int i = 0; i <= n; i ++)for(int j = 0; j <= n; j ++)if(i != j && j != k && k != i)a[i][j] = min(a[i][j], a[i][k] + a[k][j]);  //最短路memset(f, 0x3f, sizeof(f));for(int i = 1; i <= n; i ++)f[1 << (i - 1)][i] = a[0][i];  //初始化从 0 走出来for(int i = 0; i < (1 << n); i ++) {for(int j = 1; j <= n; j ++) {if(!(i & (1 << (j - 1)))) continue;  // i 为当前状态,走过 j 和 kfor(int k = 1; k <= n; k ++) {if(j == k) continue;if(!(i & (1 << (k - 1)))) continue;int s = i ^ (1 << (k - 1));  //s 是上一步的状态,最后一步是 jf[i][k] = min(f[i][k], f[s][j] + a[j][k]);}}}ans = Inf;for(int i = 1; i <= n; i ++)ans = min(ans, f[(1 << n) - 1][i] + a[i][0]);  //注意最后还要走回0printf("%d\n", ans);scanf("%d", &n);}
}

【POJ P3311】【状压DP】Hie with the Pie相关推荐

  1. poj 1185(状压dp)

    题目链接:http://poj.org/problem?id=1185 思路:状态压缩经典题目,dp[i][j][k]表示第i行状态为j,(i-1)行状态为k时最多可以放置的士兵个数,于是我们可以得到 ...

  2. poj 3254 状压dp

    E - Corn Fields Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  3. Corn Fields(POJ 3254状压dp)

    题意: n*m网格1能放0不能放 放的格子不能相邻 求一共多少种可放的方案. 分析: dp[i][j]第i行可行状态j的的最大方案数,枚举当前行和前一行的所有状态转移就行了(不放牛也算一种情况) #i ...

  4. POJ 2430 状压DP

    题意: 思路: 先预处理出所有格子的statement statement=1–>只有上边的格子被覆盖 statement=2–>只有下边的格子被覆盖 statement=3–>上下 ...

  5. Travel(HDU 4284状压dp)

    题意:给n个城市m条路的网图,pp在城市1有一定的钱,想游览这n个城市(包括1),到达一个城市要一定的花费,可以在城市工作赚钱,但前提有工作证(得到有一定的花费),没工作证不能在该城市工作,但可以走, ...

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

    Hie with the Pie 题目链接:POJ 3311 题目大意 给你 n+1 个点,其中 0 号点是特殊点. 然后两个点之间都有路径可以走. 然后你要从特殊点出发走过所有点回到特殊点,问你最短 ...

  7. POJ 1038 Bugs Integrated Inc (复杂的状压DP)

    \(POJ~1038~~*Bugs~Integrated~Inc:\) (复杂的状压DP) \(solution:\) 很纠结的一道题目,写了大半天,就想练练手,结果这手生的.其实根据之前那道炮兵阵地 ...

  8. POJ 2411 Mondriaan‘s Dream(最清楚好懂的状压DP讲解)(连通性状态压缩DP)

    poj 2411 Mondriaan's Dream(最清晰的状压DP解析) 闫氏DP大法好 我们这里是一列一列地来,因为是一个棋盘性的状态压缩DP,从哪个方向都一样 摆放的小方格总方案数 等价于 横 ...

  9. POJ 1185 炮兵阵地 【状压DP】

    <题目链接> 题目大意: 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...

  10. POJ 1321 棋盘问题(DFS 状压DP)

    用DFS写当然很简单了,8!的复杂度,16MS搞定. 在Discuss里看到有同学用状态压缩DP来写,就学习了一下,果然很精妙呀. 状态转移分两种,当前行不加棋子,和加棋子.dp[i][j]中,i代表 ...

最新文章

  1. 如何正确的学习Blender-入门到精通课程
  2. 44种模型、1200种子网,RobustART评测CNN、Transformer、MLP-Mixer谁最鲁棒?
  3. 计算机控制系统的理论,计算机控制系统理论基础.pptx
  4. JSP标签和JSTL标签注意点
  5. [POI2011]MET-Meteors 整体二分_树状数组_卡常
  6. [高速通道进阶一]如何理解高速通道的就近接入和一点接入连接全球
  7. 核弹级漏洞,把 log4j 扒给你看!
  8. 用C++解析HTTP下载下来的HTML文档
  9. C#和Java中“==”和“equals”区别
  10. 智慧屏鸿蒙系统的优势,荣耀智慧屏成热点 搭载的鸿蒙系统有何优势?
  11. mysql 浮点型详解 简书_MySQL数据类型;基本操作
  12. UVa11988 Broken Keyboard (a.k.a. Beiju Text)
  13. 虚拟化部署----USB Server
  14. 科研绘图,看这一篇就够了(内含免费送书活动)
  15. 【商品架构day9】阿里商品的标题那么长、怎么办
  16. Hyperledger Fabric(1) - 整体架构和源码结构
  17. win10linux系统双系统,win10安装linux双系统的方法是什么_win10装linux双系统的方法...
  18. 51CTO“硅谷技划”日记之一:飞向旧金山的技术集结号
  19. HTTP/1.1、HTTP/2
  20. Conflux 网络生态建设指南

热门文章

  1. vue判断什么手机打开网页及是否用QQ浏览器或者微信浏览器打开
  2. 富有人情味的入住体验
  3. css前端日记之盒子模型-----一起去未来
  4. LintCode_新手必编程50题(1-3阶段)解答与分析
  5. STM32单片机扩展下的IPUS SQPI PSRAM应用领域
  6. 芯片学生党必会的行业英文术语
  7. C# 自制微信登录窗口,100%还原,数据库(SQL Server)
  8. WPF开发经验-实现Win10虚拟触摸键盘
  9. jqgrid实现表头合并功能
  10. 在不借助中间变量的前提下,交换两个变量的值