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

这里看见一篇对动态规划解决TSP问题 描述比较细致和易懂的博客https://www.cnblogs.com/youmuchen/p/6879579.html 认认真真看了之后对个人的启发应该也是比较大的吧!(个人感觉)

所以这里我就不详细的解释过程了!

用动态规划解决,可以假设从0点出发,然后回到0点。那么用dp[ i ][ j ]表示现在处在 j 点,要去访问剩余的在集合 i 中的点,集合 i 可以用二进制数表示 例如{1,3},i 集合中剩下这两个元素二进制表示为101,转换成十进制数就是5;所以此时的dp[ i ][ j ] = dp[ { 1,3 }][ j ] = dp[ 5 ][ j ];

那么状态转移方程就是:dp[ i ][ j ]=min{dp[ i ][ j ] , dp[ i - k ][k] + dis[j][k] }(i - k)代表将k这个点从i这个集合中去掉

希望对大家还是有所帮助吧!

//交codevs的话 需要将dp数组改成dp[1<<16][16];不然会RE

 #include<iostream>
#include<algorithm>
#include<cstring> using namespace std;
const int INF = 0x3f3f3f3f;
int n, dis[][], m[][], dp[ << ][];//dp[i][j]表示在j点走完i集合中所有点返回0点的最短路
void floyed()
{
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
for (int k = ; k <= n; k++)
dis[i][j] = min(dis[i][j], dis[i][k] + m[k][j]);
}
int main()
{
ios::sync_with_stdio(false);
while (cin >> n) {
if (n == )break;
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++) {
cin >> m[i][j]; dis[i][j] = m[i][j];
}
floyed();//先用输入的矩阵跑一遍floyed求出点到点的最短路
int lim = << n;
memset(dp, -, sizeof(dp));
for (int i = ; i <= n; i++)dp[][i] = dis[i][];//当dp[i][j] i==0即点集为空集的时候下一步就是由j点返回0点的最短距离了
for (int i = ; i < lim - ; i++) {
for (int j = ; j <= n; j++) {
dp[i][j] = INF;
if (i&(<<(j-)))continue;//当前起点是j如果起点j还在集合i中 就代表此时的dp[i][j]是不合法的
for (int k = ; k <= n; k++) {//枚举剩下i中的点集合
if (!(i&( << (k - ))))continue;//如果点k不在集合i中就跳过
dp[i][j] = min(dp[i][j], dp[i ^ ( << (k - ))][k] + dis[j][k]);
//此处i ^ (1 << (k - 1)) 是将点k 从i集合中去掉后的结果 异或运算 同为0;
}
}
}
dp[lim - ][] = INF;
for (int i = ; i <= n; i++)//找出最短的那一条路径
dp[lim - ][] = min(dp[lim - ][],dp[(lim - ) ^ ( << (i - ))][i] + dis[][i]);
cout << dp[lim - ][] << endl;
}
return ;
}

POJ 3311 Hie with the Pie 兼 Codevs 2800 送外卖(动态规划->TSP问题)的更多相关文章

  1. poj 3311 Hie with the Pie (状压dp) (Tsp问题)

    这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...

  2. codevs 2800 送外卖 floyd + Tsp

    简单的状压动归 #include<cstdio> #include<algorithm> using namespace std; const int N=17; const ...

  3. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  4. POJ 3311 Hie with the Pie(DP状态压缩+最短路径)

    题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...

  5. poj 3311 Hie with the Pie dp+状压

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 ...

  6. poj 3311 Hie with the Pie (TSP问题)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 ...

  7. POJ 3311 Hie with the Pie 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

  8. POJ 3311 Hie with the Pie(状压DP + Floyd)

    题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...

  9. [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法

    主题连接:  id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...

随机推荐

  1. Android开源实战:使用MVP+Retrofit开发一款文字阅读APP

    文字控 使用MVP+Retrofit开发的一款文艺APP,它是一个非常优美的文字阅读应用,界面基本上符合material design设计规范. 在该项目中,我采用的是MVP架构,该架构目前在Andr ...

  2. 【JZOJ3873】【NOIP2014八校联考第4场第2试10.20】乐曲创作(music)

    ujfuiaty 小可可是音乐学院的一名学生,他需要经常创作乐曲完成老师布置的作业. 可是,小可可是一个懒惰的学生.所以,每次完成作业时,他不会重新创作一首新的乐曲,而是去修改上一次创作过的乐曲作为作 ...

  3. java方法重写规则 重载

    方法的重写规则 参数列表必须完全与被重写方法的相同: 返回类型必须完全与被重写方法的返回类型相同: 访问权限不能比父类中被重写的方法的访问权限更低.例如:如果父类的一个方法被声明为public,那么在 ...

  4. 用五种不同的布局方式实现“左右300px中间自适应”的效果

    float浮动 <section class="layout float"> <style media="screen"> .layou ...

  5. Facebook POP 进阶指南

    本文转自Kevin Blog Facebook 在发布了 Paper 之后,似乎还不满足于只是将其作为一个概念性产品,更进一步开源了其背后的动画引擎 POP,此举大有三年前发布的 iOS UI 框架  ...

  6. pl/sql基础知识—函数快速入门

    n  函数 函数用于返回特定的数据,当建立函数式,在函数头部必须包含return子句,而在函数体内必须包含return语句返回的数据,我们可以使用create function来建立函数,实际案例: ...

  7. Minimum Depth of Binary Tree最短深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  8. Java8 日期、时间操作

    一.简介 在Java8之前,日期时间API一直被开发者诟病,包括:java.util.Date是可变类型,SimpleDateFormat非线程安全等问题.故此,Java8引入了一套全新的日期时间处理 ...

  9. part12.5-定时器去抖

  10. Pytorch - GPU ID 指定 pytorch gpu 指定

    PyTorch 关于多 GPUs 时的指定使用特定 GPU. PyTorch 中的 Tensor,Variable 和 nn.Module(如 loss,layer和容器 Sequential) 等可 ...