POJ 3311 Hie with the Pie 兼 Codevs 2800 送外卖(动态规划->TSP问题)
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问题)的更多相关文章
- poj 3311 Hie with the Pie (状压dp) (Tsp问题)
这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...
- codevs 2800 送外卖 floyd + Tsp
简单的状压动归 #include<cstdio> #include<algorithm> using namespace std; const int N=17; const ...
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- POJ 3311 Hie with the Pie(DP状态压缩+最短路径)
题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...
- poj 3311 Hie with the Pie dp+状压
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4671 Accepted: 2471 ...
- poj 3311 Hie with the Pie (TSP问题)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4491 Accepted: 2376 ...
- POJ 3311 Hie with the Pie 最短路+状压DP
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11243 Accepted: 5963 ...
- POJ 3311 Hie with the Pie(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...
- [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
主题连接: id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...
随机推荐
- python中的open函数
open函数用于文件处理 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 1 文件句柄 = open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式打开文件 ...
- HTML 5适合小公司,适合在大平台上做内容
Web App,现在有时候也称为轻应用,不仅是通过浏览器就能打开的应用.现在随着 HTML 5 在手机端的优越性,已经慢慢称为了 Web App 的主流.Web App 除了出现在 PC 的浏览器中, ...
- oracle如何利用hostname方式连接数据库
host name方式只支持tcp/ip协议的小局域网 修改listener.ora中的如下信息 (SID_DESC = (GLOBAL_DBNAME = ur_hostname) --你的机器名 ( ...
- PHPCMS快速建站系列之标签调用出错
{pc:content action="position" posid="24" order="listorder ASC" thumb=& ...
- nodeJs学习-11 multer中间件,解析post文件,上传文件
const express=require('express'); const bodyParser=require('body-parser'); const multer=require('mul ...
- Flask 第二篇
Flask 中的 Render Redirect HttpResponse 1.Flask中的HTTPResponse 在Flask 中的HttpResponse 在我们看来其实就是直接返回字符串 2 ...
- python 顺序传入
- WPF 2048游戏的实现
原文:WPF 2048游戏的实现 前几天空闲的时候,实现了一个2048游戏.除了可以设置行数和列数之外,支持修改显示名称,比如下面,改成神雕侠侣中的角色名称: 游戏 ...
- Laravel 的HTTP请求#
获取请求# 要通过依赖注入的方式来获取当前HTTP请求的实例,你应该在控制器方法中类型提示Illuminate\Http\Request 传入的请求的实例通过 服务容器自动注入: <?php n ...
- iphone开发中调用系统打电话功能
iphone开发中调用打电话功能,一般有2种: 1.系统的打电话代码,不返回当前程序: Java代码 [[UIApplication sharedApplication] openURL:[NSURL ...