POJ3311Hie with the Pie(floyd传递+DP,状态压缩)
问题
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
- floyd闭包传递。
- 之前做过差不多的题,主要是保存以及访问过的和目前停止的地方。
- 需要强制起点为0。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int inf=;
int dp[][],dis[][],n,ans;
void floyd()
{
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
void getdp()
{
for(int i=;i<(<<(n+));i++){
for(int j=;j<=n;j++){
if(i&(<<j)){
if(i==(<<j)) dp[i][j]=dis[][j];
else{
dp[i][j]=inf;
for(int k=;k<=n;k++)
if((i&(<<k)&&k!=j))
dp[i][j]=min(dp[i][j],dp[i^(<<j)][k]+dis[k][j]);
}
}
}
}
}
int main()
{
int i,j,k;
while(~scanf("%d",&n)){
if(n==) return ;
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&dis[i][j]); floyd(); getdp(); ans=inf;
for(i=;i<=n;i++) ans=min(ans,dp[(<<(n+))-][i]+dis[i][]);
printf("%d\n",ans);
}
return ;
}
POJ3311Hie with the Pie(floyd传递+DP,状态压缩)的更多相关文章
- ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)
题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:学生要完成各科作业, 给出各科老师给出交作业的期限和学生完成该科所需时间, 如果逾期一 ...
- hdu_4352_XHXJ's LIS(数位DP+状态压缩)
题目连接:hdu_4352_XHXJ's LIS 题意:这题花大篇篇幅来介绍电子科大的一个传奇学姐,最后几句话才是题意,这题意思就是给你一个LL范围内的区间,问你在这个区间内最长递增子序列长度恰为K的 ...
- hdu 4352 数位dp + 状态压缩
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【bzoj1076】[SCOI2008]奖励关 期望dp+状态压缩dp
题目描述 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物,每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的宝物以后也不能再 ...
- hdu4336 Card Collector(概率DP,状态压缩)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
- dp状态压缩
dp状态压缩 动态规划本来就很抽象,状态的设定和状态的转移都不好把握,而状态压缩的动态规划解决的就是那种状态很多,不容易用一般的方法表示的动态规划问题,这个就更加的难于把握了.难点在于以下几个方面:状 ...
- 洛谷 1052 dp 状态压缩
洛谷1052 dp 状态压缩 传送门 (https://www.luogu.org/problem/show?pid=1052#sub) 做完这道题之后,感觉涨了好多见识,以前做的好多状压题目都是将一 ...
随机推荐
- SQL Injection(Blind)
SQL Injection(Blind),即SQL盲注,与一般注入的区别在于,一般的注入攻击者可以直接从页面上看到注入语句的执行结果,而盲注时攻击者通常是无法从显示页面上获取执行结果,甚至连注入语句是 ...
- C学习笔记-枚举
枚举定义 可以使用枚举(enumerated type)声明代表整数常量的符号名称,关键字enum创建一个新的枚举类型 实际上,enum常量是int类型的 枚举的本质就是int型的常量 enum sp ...
- 论文阅读 | Trojaning Attack on Neural Networks
对神经网络的木马攻击 Q: 1. 模型蒸馏可以做防御吗? 2. 强化学习可以帮助生成木马触发器吗? 3. 怎么挑选建立强连接的units? 本文提出了一种针对神经元网络的木马攻击.模型不直观,不易被人 ...
- VisualBasic文件与目录管理FileSystem 类
注解 下表列出了涉及 My.Computer.FileSystem 对象的任务示例. 功能 查看 从文本文件读取 如何:读取文本文件 从带分隔符的文本文件中读取 如何:读取逗号分隔的文本文件 从固定宽 ...
- adb工具介绍与安装
一天笑嘻嘻是一名测试人员,想了解Android的测试方法,于是,就找到了小测试. 笑嘻嘻:身为一名测试人员需要了解ADB的哪些内容? 小测试:了解原理和简单的命令使用就可以了. 笑嘻嘻:你有毒啊,都了 ...
- Elasticsearch Metric聚合
首先查看index文档信息 $ curl -XGET "http://172.16.101.55:9200/_cat/indices?v" 输出 health status ind ...
- PTA(Advanced Level)1011.World Cup Betting
With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...
- Linux下面查看网卡的信息
查看linux下面网卡的速度信息 Study From 百度知道 (懒得翻墙) 1. centos机器 安装的比较全(个人比较懒 没有使用core最小化安装, 避免出问题麻烦 公司网络太垃圾) 使用 ...
- 设计模式:备忘录模式(Memento)
个人比较喜欢玩单机游戏,什么仙剑.古剑.鬼泣.使命召唤.三国无双等等一系列的游戏我都玩过(现在期待凡人修仙传),对于这些游戏除了剧情好.场面大.爽快之外,还可以随时存档,等到下次想玩了又可以从刚开始的 ...
- Intellij IDEA神器好用到飞起来的配置
IDEA 全称 IntelliJ IDEA,是java编程语言开发的集成环境. IntelliJ在业界被公认为最好的java开发工具之一, 尤其在智能代码助手.代码自动提示.重构.J2EE支持 ...