题目链接:http://poj.org/problem?id=3311

Hie with the Pie
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions:12225   Accepted: 6441

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.

题目大意:从 0 点出发,经过其他 n 点(允许重复经过),最后回到 0 点,要求输出最短距离。

思路:

1.属于经典的旅行商问题(TSP问题):从一个点出发可重复的经过其他至少一次,最后回到该点,问如何选择路线使得路径最短。TSP问题需要用到状压dp来记录状态,这里有一篇博客:TSP问题总结归纳以及例题

2.还需要先跑一遍floyd得到任意两点之间的最短距离。因为图中两点之间并不一定是最短的路径。

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define mem(a, b) memset(a, b, sizeof(a))
#define LL long long
using namespace std;
const int MAXN = ;
const int inf = 0x3f3f3f3f; int n;
LL map[MAXN][MAXN], dis[MAXN][MAXN];
LL dp[ << ][MAXN]; //表示达到 i 状态时,最后访问的位置是 j void floyd()
{
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], map[i][k] + map[k][j]);
} int main()
{
while(scanf("%d", &n) != EOF)
{
if(n == )
break;
for(int i = ; i <= n; i ++)
{
for(int j = ; j <= n; j ++)
{
scanf("%lld", &map[i][j]);
}
}
mem(dis, inf);
floyd(); //处理出任意两点之间的最短距离
for(int i = ; i <= (( << n) - ); i ++)//枚举状态
{
int S = i; //state
for(int j = ; j <= n; j ++)
{
if(S == ( << (j - ))) //只经过 j 这个点
dp[S][j] = dis[][j];
else
{
dp[S][j] = inf;
for(int k = ; k <= n; k ++) //与 j 不同的点
{
if(k != j && (S & ( << (k - ))))
dp[S][j] = min(dp[S][j], dp[S ^ ( << (j - ))][k] + dis[k][j]);
}
}
}
}
LL ans = dp[( << n) - ][] + dis[][];
for(int i = ; i <= n; i ++)
ans = min(ans, dp[( << n) - ][i] + dis[i][]);
printf("%lld\n", ans);
}
return ;
}

POJ3311

POJ3311 Hie with the Pie 【状压dp/TSP问题】的更多相关文章

  1. 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]

    题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...

  2. POJ 3311 Hie with the Pie (状压DP)

    dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...

  3. East Central North America 2006 Hie with the Pie /// 状压dp oj22470

    题目大意: 输入n,有n个地方(1~n)需要送pizza pizza点为0点 接下来n+1行每行n+1个值 表示 i 到 j 的路径长度 输出从0点到各点送pizza最后回到0点的最短路(点可重复走) ...

  4. Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)

    题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...

  5. poj3311 Hie with the Pie (状态压缩dp,旅行商)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3160   Accepted: 1613 ...

  6. zoj 3471 Most Powerful(状压dp+Tsp问题+连续性问题)

    上来直接一波敲键盘,直接套Tsp问题的代码 然后WA 发现貌似这道题没有连续性. Tsp问题是一条路径,一个点到另一个点,多了一个限制,所以就需要加多一维 而这道题没有限制,也就是说那一维不需要加,我 ...

  7. poj 2288 Islands and Bridges (状压dp+Tsp问题)

    这道题千辛万苦啊! 这道题要涉及到当前点和前面两个点,那就设dp[state][i][j]为当前状态为state,当前点为i,前一个点为j 这个状态表示和之前做炮兵那题很像,就是涉及到三个点时,就多设 ...

  8. light oj 1057 状压dp TSP

    #include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...

  9. 【POJ3311】Hie with the Pie(状压DP,最短路)

    题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...

随机推荐

  1. [CQOI2016]手机号码 数位DP

    [CQOI2016]手机号码 用来数位DP入门,数位DP把当前是否需要限制取数范围(是否正在贴着临界值跑,即下面的limited)和一切需要满足的条件全部塞进记忆化搜索参数里面就好了,具体情况转移便好 ...

  2. Ubuntu start:未知任务:mysql

    在Ubuntu环境下,下载安装mysql但是到最后启动的时候却显示这一句话: start :未知任务:mysql 很纳闷,明明按照教程一步一步写的.后来才想起来,万能的方法,在前面加权限 sudo s ...

  3. AE开发之shp转txt

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. CF1197C

    CF1197C 题意: 有一个递增的数列,把它分成k段,找到这样的分段方法,即每段最大值减最小值的和最小 解法: 分成k段,即要加k-1个隔断,这k-1个隔断,能隔开差值最大的几个,那最后得到分隔后的 ...

  5. 令人窒息的操作,nodejs面向对象。

    // async函数返回一个 Promise 对象,可以使用then方法添加回调函数. // 当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句. clas ...

  6. HTTP之持久连接

    HTTP/1.1 允许 HTTP 设备在事务处理结束之后将 TCP 连接保持在打开状态,以便为未来的 HTTP 请求重用现存的连接.在事务处理结束后仍然保持在打开状态的 TCP 连接被称为持久连接.非 ...

  7. ArcGIS超级工具SPTOOLS-锐角检查,获得内角并判断是否凸多边形,获得线(面)两个折点方向

    1.1  锐角检查 操作视频: https://weibo.com/tv/v/HCNNXhm5F?fid=1034:4392479009475111 可以是面.线夹角锐角检查 输出锐角点的位置和角度( ...

  8. Mysql触发器详解以及union的使用

    ---恢复内容开始--- Mysql触发器定义: 当一个表中有insert update delete事件发生,触发一个事件,执行一段代码.作用: 同步数据创建: create trigger 名称 ...

  9. [转]用代码访问 Https

    可以参考: https://blog.csdn.net/irokay/article/details/78801307 跳过证书验证方法 HttpClient简介HTTP 协议可能是现在 Intern ...

  10. java.util.HashTable (JDK1.8)

    1.Hashtable 特性(先总结下面会详细讲的): 1.Hashtable 存储的内容是键值对(key-value)映射,其底层实现是一个Entry数组+链表. 2.Hashtable是线程安全( ...