加油 ( •̀ ω •́ )y!

POJ 3311 Hie with the Pie 兼 Codevs 2800 送外卖(动态规划->TSP问题)

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

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 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 }

 

posted @ 2018-08-15 17:37  皮皮虎  阅读(225)  评论(0编辑  收藏  举报