博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3311 Hie with the Pie 兼 Codevs 2800 送外卖(动态规划->TSP问题)
阅读量:6719 次
发布时间:2019-06-25

本文共 3755 字,大约阅读时间需要 12 分钟。

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

30 1 10 101 0 1 210 1 0 1010 2 10 00

Sample Output

8

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

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

用动态规划解决,可以假设从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

1 #include
2 #include
3 #include
4 5 using namespace std; 6 const int INF = 0x3f3f3f3f; 7 int n, dis[20][20], m[20][20], dp[1 << 11][20];//dp[i][j]表示在j点走完i集合中所有点返回0点的最短路 8 void floyed() 9 {10 for (int i = 0; i <= n; i++)11 for (int j = 0; j <= n; j++)12 for (int k = 0; k <= n; k++)13 dis[i][j] = min(dis[i][j], dis[i][k] + m[k][j]);14 }15 int main()16 {17 ios::sync_with_stdio(false);18 while (cin >> n) {19 if (n == 0)break;20 for (int i = 0; i <= n; i++)21 for (int j = 0; j <= n; j++) {22 cin >> m[i][j]; dis[i][j] = m[i][j];23 }24 floyed();//先用输入的矩阵跑一遍floyed求出点到点的最短路25 int lim = 1 << n;26 memset(dp, -1, sizeof(dp));27 for (int i = 0; i <= n; i++)dp[0][i] = dis[i][0];//当dp[i][j] i==0即点集为空集的时候下一步就是由j点返回0点的最短距离了28 for (int i = 1; i < lim - 1; i++) {29 for (int j = 1; j <= n; j++) {30 dp[i][j] = INF;31 if (i&(1<<(j-1)))continue;//当前起点是j如果起点j还在集合i中 就代表此时的dp[i][j]是不合法的32 for (int k = 1; k <= n; k++) {
//枚举剩下i中的点集合33 if (!(i&(1 << (k - 1))))continue;//如果点k不在集合i中就跳过34 dp[i][j] = min(dp[i][j], dp[i ^ (1 << (k - 1))][k] + dis[j][k]);35 //此处i ^ (1 << (k - 1)) 是将点k 从i集合中去掉后的结果 异或运算 同为0;36 }37 }38 }39 dp[lim - 1][0] = INF;40 for (int i = 1; i <= n; i++)//找出最短的那一条路径41 dp[lim - 1][0] = min(dp[lim - 1][0],dp[(lim - 1) ^ (1 << (i - 1))][i] + dis[0][i]);42 cout << dp[lim - 1][0] << endl;43 }44 return 0;45 }

 

转载于:https://www.cnblogs.com/wangrunhu/p/9482829.html

你可能感兴趣的文章
用sql语句对access数据库进行多条件查询
查看>>
php操作ini配置文件
查看>>
dataguard主备延迟多长时间的查询方法
查看>>
[Array]628. Maximum Product of Three Numbers
查看>>
C++函数模板&类模板
查看>>
spring事件广播
查看>>
javascript事件委托和jquery事件委托
查看>>
使用ReaderWriterLock类实现多用户读/单用户写同步
查看>>
MySQL--Basic(一)
查看>>
(转)CSS字体大小: em与px、pt、百分比之间的对比
查看>>
C语言的关键字
查看>>
喷水装置(一)NYOJ6
查看>>
填充与步幅
查看>>
bzoj 1911 特别行动队
查看>>
关于PHPExcel类占用内存问题
查看>>
hadoop分布式存储(1)-hadoop基础概念
查看>>
Mac svn使用学习-1-简介
查看>>
浅谈IT技术选型和未来技术发展趋势
查看>>
JS怎么创建一个类?
查看>>
I00017 生成9开头的按位递减数
查看>>