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. 模板内置函数(HTML)

    模板内置函数 注意:1.html书写避免多余的空格,否则可能无法被识别 2.模板是用来渲染的不要用来处理逻辑 后台ctime=datetime.datatime.now() {{ctime|date: ...

  2. PLAY2.6-SCALA(六) 异步处理结果

    1.创建异步的controller Play是一个自底向上的异步框架,play处理所有的request都是异步.非阻塞的.默认的方式是使用异步的controller.换句话说,contrller中的应 ...

  3. 【C++】关于map的遍历 删除

    int main(int argc, char* argv[]) { map<string, string> mapData; mapData["a"] = " ...

  4. postman测试接口各种类型传值

    postman测试接口各种类型传值 标签: postman测试 json串 Map 2018年01月27日 02:32:00 145人阅读 评论(0) 收藏 举报 1.Map类型或实体类类型传值,即j ...

  5. [React Native]StatusBar的使用

    StatusBar是React Native 0.20 新增的跨平台组件,它可以用来设置并动态改变设备的状态栏显示特性. 虽然说是跨平台的组件, 但是有些属性不是跨平台的 ,我们需要注意下.因为IOS ...

  6. UVa 10599【lis dp,记忆化搜索】

    UVa 10599 题意: 给出r*c的网格,其中有些格子里面有垃圾,机器人从左上角移动到右下角,只能向右或向下移动.问机器人能清扫最多多少个含有垃圾的格子,有多少中方案,输出其中一种方案的格子编号. ...

  7. 5-2 正则表达式及其re模块

    一 正则表达式 在线测试工具 http://tool.chinaz.com/regex/ 字符 量词 贪婪匹配 贪婪匹配:在满足匹配时,匹配尽可能长的字符串,默认情况下,采用贪婪匹配,<.*&g ...

  8. 权重衰减(weight decay)与学习率衰减(learning rate decay)

    本文链接:https://blog.csdn.net/program_developer/article/details/80867468“微信公众号” 1. 权重衰减(weight decay)L2 ...

  9. @codechef - TREEPATH@ Decompose the Tree

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵无根树,每个节点上都写了一个整数. 你的任务就是统计有多 ...

  10. @雅礼集训01/06 - T3@ math

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给出 n, m, x,你需要求出下列式子的值: \[\sum_{ ...