Hie with the Pie

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11243   Accepted: 5963

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
题目大意: 
一个送外卖的人,从0点出发,要经过所有的地点然后再回到店里(就是0点),求最少花费的代价。 
输入 
1<=n<=10 ,n表示要去的n个地点,包括店里(0点)一共右n+1个点
接下来n+1行用矩阵来描述路径,用map[i][j]存储,表示从i到j的距离为map[i][j] 题解:
1、暴力:

因为要经过所有的点,直接用dfs或最短路径来求肯定是不行的,要枚举第一个到达的点的所有情况,每种情况都有不同路径。例如有3个点,假设1为第一个到达的点,那么不同的顺序有0->1->2->3->0
0->1->3->2->0
所以时间复杂度为o(n!)
2、最短路+DP
仔细观察搜索解法的过程,其实是有很多重复计算的。
比如从0点出发,经过1,2,3,4,5点后回到0点。那么
0->1->2->(3,4,5三个点的排列)->0与
0->2->1->(3,4,5三个点的排列)->0
就存在重复计算3,4,5三点的排列)->0路径集上的最短路径。只要我们能够将这些状态保存下来就能够降低一部分复杂度。DP实现 状态压缩:对所有的点543210,可以用一个长度为6的二进制数表示,对去过的点可以用1来表示,没有去过的点用0表示,显然,二进制数可以很好的储存走过的状态
比如说:0->1->2->5,对应的二进制数为100111,存储的时用十进制数存储,100111对应的十进制数是39 dp[i][j]表示i这个状态下,目标是j的最短路,这里的i就是压缩的状态,是用二进制表示每个地点是否去过(在i状态下到达的点已经包含点j)
    用floyd求最短路径,dis[i][j]表示点i到目标点j的最短路径

    状态转移方程:dp[i][j] = min(dp[i][j],dp[i'][k]+dis[k][j])。 i’表示i的不包含j的子集,dis[k][j]表示从k到j的最短距离。
#include<iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//dp[i][j]表示i这个状态下,目标是j的最短路,这里的i就压缩的状态,是用二进制表示每个地点是否去过,
//注意:在i状态下到达的点已经包含点j
int map[][],dis[][],dp[<<][];//map记录路径,dis[i][j]记录点i到j的最短路 int main()
{
int n,i,j,k;
while(~scanf("%d",&n))//包括0点,一个有n+1个点要去
{
if(n==)
break;
for(i = ; i<=n; i++)
for(j = ; j<=n; j++)
{
scanf("%d",&map[i][j]);
dis[i][j] = map[i][j];//初始化dis[i][j]的值
}
for(j = ; j<=n; j++)
{
for(i = ; i<=n; i++)
{
for(k = ; k<=n; k++)
{
if(dis[i][j]>dis[i][k]+map[k][j])//更新dis
dis[i][j] = dis[i][k]+map[k][j];
}
}
} memset(dp,-,sizeof(dp));
dp[][] = ;//表示从第0个点到第0个点的距离为0
for(i = ; i<<<(n+); i++)
{
i = i|;//改变状态,表示第i点去过
for(j = ; j<=n; j++)
{
if(dp[i][j]!=-)//在i状态下,点j已经去过,距离为dp[i][j],但还要考虑i状态是否是最短
{
for(k = ; k<=n; k++)//遍历,k点是i状态要经过的下一个点
{
//(1<<k)|i,将数字i在二进制下从右往左的第k+1位更改成1,表示经过这个点
if(j!=k && (dp[(<<k)|i][k]==- || dp[(<<k)|i][k]>dp[i][j]+dis[j][k]))
//如果i状态下,k点还未经过,或者已经过k点的距离比dp[i][j]+dis[j][k]的距离大,这里的点dp[i][j]是还未过k点的状态
dp[(<<k)|i][k]=dp[i][j]+dis[j][k];
}
}
}
}
printf("%d\n",dp[(<<(n+))-][]);//(1<<(n+1))所有点都去过,并且最后回到0点
} return ;
}

POJ 3311 Hie with the Pie 最短路+状压DP的更多相关文章

  1. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  2. HDU 4568 Hunter 最短路+状压DP

    题意:给一个n*m的格子,格子中有一些数,如果是正整数则为到此格子的花费,如果为-1表示此格子不可到,现在给k个宝藏的地点(k<=13),求一个人从边界外一点进入整个棋盘,然后拿走所有能拿走的宝 ...

  3. 最短路+状压DP【洛谷P3489】 [POI2009]WIE-Hexer

    P3489 [POI2009]WIE-Hexer 大陆上有n个村庄,m条双向道路,p种怪物,k个铁匠,每个铁匠会居住在一个村庄里,你到了那个村庄后可以让他给你打造剑,每个铁匠打造的剑都可以对付一些特定 ...

  4. 【BZOJ1097】[POI2007]旅游景点atr 最短路+状压DP

    [BZOJ1097][POI2007]旅游景点atr Description FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺 ...

  5. HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP

    题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others)  ...

  6. hdu3247Resource Archiver (AC自动机+最短路+状压dp)

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submis ...

  7. poj 3311 Hie with the Pie (TSP问题)

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

  8. poj 3311 Hie with the Pie dp+状压

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

  9. 【CodeVS2800】 送外卖 最短路+状压DP

    首先求出各点之间的最短路,floyed即可,注意是0-n. 然后考虑状压,f[i][j]表示状态为i时访问j点时的最短路和,1表示访问,0表示未访问,然后第j个点所在的位置就是(1<<j) ...

随机推荐

  1. 【剑指Offer面试编程题】题目1511:从尾到头打印链表--九度OJ

    题目描述: 输入一个链表,从尾到头打印链表每个节点的值. 输入: 每个输入文件仅包含一组测试样例. 每一组测试案例包含多行,每行一个大于0的整数,代表一个链表的节点.第一行是链表第一个节点的值,依次类 ...

  2. 彻底搞懂 JS 中 this 机制

    彻底搞懂 JS 中 this 机制 摘要:本文属于原创,欢迎转载,转载请保留出处:https://github.com/jasonGeng88/blog 目录 this 是什么 this 的四种绑定规 ...

  3. SpringAOP原理分析

    目录 Spring核心知识 SpringAOP原理 AOP编程技术 什么是AOP编程 AOP底层实现原理 AOP编程使用 Spring核心知识 Spring是一个开源框架,Spring是于2003年兴 ...

  4. CSP-S2019 爆炸记

    DAY -1 停课的第五天.早上来机房教练居然不在,先看了一道憨题,发现ST表+二分查找nlogn水过,然后发现单调栈可以O(n),肥肠开心 打了走人. 然后就开始颓了(逃 颓了一会之后看愤怒的小鸟这 ...

  5. [FBCTF2019]Products Manager

    基于约束的SQL攻击 一.知识点: 1.数据库字符串比较: 在数据库对字符串进行比较时,如果两个字符串的长度不一样,则会将较短的字符串末尾填充空格,使两个字符串的长度一致,比如,字符串A:[Strin ...

  6. leetcode844 Backspace String Compare

    """ Given two strings S and T, return if they are equal when both are typed into empt ...

  7. Lesson 2 Spare that spider

    How much of each year do spiders killing insects? Why, you may wonder, should spiders be our friends ...

  8. Jquery实现横向tab切换

    //需求:鼠标放在不同的导航栏上,下面显示的内容自动切换 //代码如下 <!DOCTYPE html> <html lang="en"> <head& ...

  9. Java的SpringMVC执行流程

    SpringMVC找Controller流程 1.扫描整个项目(Spring已经做了)定义一个Map集合. 2.拿到所有加了@Controller注解的类. 3.遍历类里面的所有方法对象. 4.判断方 ...

  10. is application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem

    最近试着了解 c++,接触到了QT,写了一个测试程序,在开发环境下正常后移到非开发环境,报错 网上找资料说是少了platforms文件夹中的dll,把里面所有的dll复制到执行程序目录,还是提示,继续 ...