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 ;
}

最新文章

  1. iOS开发中的错误整理,AFN框架和MJRefresh框架搭配应该注意的问题
  2. ACM 数独
  3. 初学CDQ分治-NEU1702
  4. BOOST的AUTO link机制以及配置
  5. 双系统修改启动项顺序&amp;&amp;&amp;修改开机启动等待时间
  6. http://www.blogjava.net/nokiaguy/category/37087.html
  7. linux Ubuntu安装后没有引导 解决方案
  8. poj2436,poj3659,poj2430
  9. java Math.random()随机数的产生
  10. [转]openlayer+geoserver实现WFS操作
  11. Netbeans搭建Android环境
  12. esyui datagrid 水平方向下方出来滚动条的原因是因为使用了同一列名
  13. 怎么把微信里的文件发到QQ?
  14. VuePress
  15. windows10安装MongoDB服务启动不了 &#128512;✅已解决!
  16. JS判断是否是数组的四种做法
  17. Zookeeper入门(二)之基础
  18. 通过iLO进行Zabbix监控——针对HP服务器集成
  19. 170321、Spring+Quartz实现定时任务的配置方法
  20. iOS开发多线程篇 07 —GCD的基本使用

热门文章

  1. Python 基于 NLP 的文本分类
  2. Directx11教程(60) tessellation学习(2)
  3. Error configuring application listener of class org.springframework.web.context.ContextLoaderListene 标签: tomcat
  4. MongoDB sharding 集合不分片性能更高?
  5. openjudge dp水题记录
  6. java返回结果集封装
  7. thinkphp5.0 路由规则配置
  8. 在centos搭建git服务器时,不小心把/home/git目录删除了,我是怎么恢复的
  9. jmeter响应代码为乱码
  10. HLSL像素着色器