D - Hie with the Pie

Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

 

Input

 

Output

 

Sample Input

 

Sample Output

 

Hint

 

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

题意:
一个人从起点出发去n个地方送货,最后回到起点,给出每两个点之间的距离计算最短路程。

代码:

 /*
dp[i][j]表示状态i时到达j点的时间,dp[i][j]=min(dp[i][j],dp[h][k]+d[k][j]),h表示的状态中没有j点
d[k][j]表示k点到j点的距离,由于是求最短要用一个flody算法算出最短距离
*/
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<queue>
#include<stack>
using namespace std;
int n;
int d[][];
int dp[<<][];
int min(int x,int y)
{
return x<y?x:y;
}
int main()
{
while(scanf("%d",&n))
{
if(n==) break;
n+=;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
scanf("%d",&d[i][j]);
}
for(int k=;k<n;k++)
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
if(d[i][j]>d[i][k]+d[k][j])
d[i][j]=d[i][k]+d[k][j];
}
for(int i=;i<(<<n);i++)
{
for(int j=;j<n;j++)
{
if(i==(<<j))
dp[i][j]=d[][j]; //从0点可以直接到达的点
else if(i&(<<j)) //当i状态中包含j点时
{
dp[i][j]=; //初始化
for(int k=;k<n;k++)
{
if((j!=k)&&(i&(<<k))) //k与j非同一个点,i状态中包含k点
dp[i][j]=min(dp[i][j],dp[i^(<<j)][k]+d[k][j]); //不包含j点但有k点的状态
//加上d[k][j]
}
}
}
}
int ans=;
for(int i=;i<n;i++)
ans=min(ans,dp[(<<n)-][i]+d[i][]);
cout<<ans<<endl;
}
return ;
}

状态压缩 DP的更多相关文章

  1. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  2. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  3. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  4. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  5. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  6. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  7. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  8. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

  10. HDU 4511 (AC自动机+状态压缩DP)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...

随机推荐

  1. SQLServer 维护脚本分享(09)相关文件读取

    /********************[读取跟踪文件(trc)]********************/ --查看事件类型描述 SELECT tc.name,te.trace_event_id, ...

  2. HDU 4821 String (HASH)

    题意:给你一串字符串s,再给你两个数字m l,问你s中可以分出多少个长度为m*l的子串,并且子串分成m个长度为l的串每个都不完全相同 首先使用BKDRHash方法把每个长度为l的子串预处理成一个数字, ...

  3. Java会出现"unreachable code"错误的几个例子

    public class exam { static int num=5; static int m1(){ try{ num=6; throw new NullPointerException(); ...

  4. 去除android手机滚动条

    方法1:::-webkit-scrollbar{display: none;} 方法2:::-webkit-scrollbar{height:0; width:0:}

  5. wpf,记录一下颜色设置的2中方法,,,

    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color ...

  6. Android实现全屏的三种方式

    一.通过代码 requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题栏 getWindow().setFlags(WindowManager.Lay ...

  7. 虚拟机CentOS-mini安装完成后的网络设置

    系统环境:虚拟机, CentOS-mini,x86-64, 1. 主机名设置 涉及的文件: /etc/hostname;  /etc/sysconfig/network 1.1 在/etc/hostn ...

  8. HDU1402 A * B Problem Plus(FFT)

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 初学FFT. http://www.cnblogs.com/WABoss/p/FFT_Note.html ...

  9. 百度地图API使用记录

    用户数据图层的总教程: 就是把用户数据存到LBS云里面,应用从云里面读数据 http://developer.baidu.com/map/jsdevelop-9.htm 上传数据的地方: http:/ ...

  10. 【SAP BO】BOE 4.1版本新特性

    为了更好地向用户推广BusinessObjects BI 4.1版本的新特性,SAP公司宣布将在2012年中旬推出针对BusinessObjects BI 4.0的功能补丁程序(Feature Pac ...