状态压缩 DP
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的更多相关文章
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
- HDU 4511 (AC自动机+状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...
随机推荐
- 4类 JavaScript 内存泄露及如何避免
原文:4 Types of Memory Leaks in JavaScript and How to Get Rid Of Them笔记:涂鸦码龙 译者注:本文并没有逐字逐句的翻译,而是把我认为重要 ...
- SQL初级第三课(下)
我们续用第三课(上)的表 辅助表 Student Course Score Teacher Sno ...
- 01 HTML基础
HTML就是超文本标记语言的简写,是最基础的网页语言. 通过标签定义的语言,代码都是由标签所组成的.(最重要的标签是定义标题.段落和换行的标签) 不区分大小写 head部分是给html页面增加一些辅助 ...
- 20145223《Java程序程序设计》第7周学习总结
20145223 <Java程序设计>第7周学习总结 教材学习内容总结 ·时间的度量: (1)格林威治标准时间 (2)世界时 (3)国际原子时 (4)世界协调时间 (5)Unix时间 (6 ...
- MySql 查询数据库中所有表名
查询数据库中所有表名select table_name from information_schema.tables where table_schema='csdb' and table_type= ...
- 矩阵快速幂 ZOJ 3497 Mistwald
题目传送门 题意:看似给了一个迷宫,每个点能传送到4个地方,问在P时间能否到达终点 分析:其实是一个有向图,可以用邻接矩阵存图,连乘P次看是否能从1到n*m,和floyd的传递背包思想一样 #incl ...
- Python学习笔记08
正则表达式包re match,search,sub re.match(pattern, string, flags=0) re.search(pattern, string, flags=0) r ...
- mvn exec用法,运行jar后台驻留进程
java工程如果打包成war,那依赖的jar包都会被包含进去. 不过如果开发java的后台驻留进程,那一般会打包成jar包的形式,要想在运行进程的时候找到所有的依赖包,基本有如下两种方式: 方式一: ...
- POJ3686 The Windy's(最小费用最大流)
题目大概说要用m个工厂生产n个玩具,第i个玩具在第j个工厂生产要Zij的时间,一个工厂同一时间只能生成一个玩具,问最少的用时. 这题建的图不是很直观.. 源点向玩具连容量1费用0的边 将每个工厂拆成n ...
- HDU3157 Crazy Circuits(有源汇流量有上下界网络的最小流)
题目大概给一个电路,电路上有n+2个结点,其中有两个分别是电源和负载,结点们由m个单向的部件相连,每个部件都有最少需要的电流,求使整个电路运转需要的最少电流. 容量网络的构建很容易,建好后就是一个有源 ...