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. hdu 1063(java写高精度)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1063 思路:最近刚学了java,然后就迫不及待想试试java写大数的好处了,呵呵,果然是很方便啊! i ...

  2. 滑动listview隐藏和显示顶部布局

    需求: 1.listview向下滑动时,隐藏顶部布局 2.listview向上滑动到最上面,显示顶部布局 3.顶部布局的隐藏和显示有过渡效果 4.第一次加载listview时,顶部不要隐藏 布局: 注 ...

  3. cdh环境下,spark streaming与flume的集成问题总结

    文章发自:http://www.cnblogs.com/hark0623/p/4170156.html  转发请注明 如何做集成,其实特别简单,网上其实就是教程. http://blog.csdn.n ...

  4. Java和数据库时间格式化格式

    JAVA: yyyy-MM-dd HH-mm-ss(大小HH表示24小时制); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd&q ...

  5. DSP using Matlab 书内练习Example 2.1

    先上代码,后出结果图. a. n = [-5:5]; x=2*impseq(-2,-5,5) - impseq(4,-5,5); set(gcf,'Color',[1,1,1]) % 改变坐标外围背景 ...

  6. vim使用01

    安装与基础配置 iTerm快捷操作 清屏: <C l>/<W k> 剪切: <W x> 复制: <W v> 新增窗口: <W d> 切换窗口 ...

  7. 【虚拟机】苹果虚拟机mac10.11.6+Xcode8.1

    [虚拟机]苹果虚拟机mac10.11.6+Xcode8.1本虚拟机加装Xcode8.1,方便大家更好学习Swift3.0语言以及iOS开发.安装注意事项:第一步:确认硬件:1.确认主板以及cpu支持虚 ...

  8. 04 DOM一窥

    BOM 浏览器对象模型 * window       alert(); 弹出框 confirm() 询问框 setInterval("run()",1000); 每隔1秒执行run ...

  9. Webstorm & PhpStorm

    WebStorm注册码 User Name: EMBRACE License Key: ===== LICENSE BEGIN ===== 24718-12042010 00001h6wzKLpfo3 ...

  10. 全排列 UVA 11525 Permutation

    题目传送门 题意:训练指南P248 分析:逆向考虑,比如一个全排列:7345261,它也可以表示成题目中的形式,第一个数字7是由6 * (7 - 1)得到的,第二个数字3有2 * (7 - 2)得到, ...