poj3311Hie with the Pie
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7599 | Accepted: 4088 |
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
Source
分析:很经典的状压dp问题,不过我当时状态想错了,以为用一维就可以表示,这样导致的后果就是可以直接从一个点跳到另一个毫不相干的点上去,事实上,我们需要用二维表示状态,设dp[s][i]表示状态s的情况下最终走到i的最短距离,s是一串二进制串,每一位表示是否经过第i个点,状态转移方程很容易看出来,我们可以这样分析:另取一个已经经过的点j,如果我们要达到i,我们可以用没经过i的状态但已经走到j的状态,加上i到j的距离更新,具体看代码:
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm> using namespace std; int n, d[][], dp[ << ][], ans; int main()
{
while (scanf("%d", &n) && 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][k] + d[k][j] < d[i][j])
d[i][j] = d[i][k] + d[k][j];
for (int s = ; s <= ( << n) - ; s++)
{
for (int i = ; i <= n; i++)
{
if (s & ( << (i - )))
{
if (s == ( << (i - )))
dp[s][i] = d[][i];
else
{
dp[s][i] = ;
for (int j = ; j <= n; j++)
if ((s & ( << (j - ))) && i != j)
dp[s][i] = min(dp[s][i], dp[s ^ ( << (i - ))][j] + d[j][i]);
}
}
}
}
ans = ;
for (int i = ; i <= n; ++i)
ans = min(dp[( << n) - ][i] + d[i][], ans);
printf("%d\n", ans);
} return ;
}
poj3311Hie with the Pie的更多相关文章
- POJ3311Hie with the Pie(floyd传递+DP,状态压缩)
问题 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...
- [No0000A2]“原始印欧语”(PIE)听起来是什么样子?
"Faux Amis"节目中经常提到"原始印欧语"(PIE)——"Proto-Indo-European". 我们说过,英语,法语中的&qu ...
- poj3311 Hie with the Pie (状态压缩dp,旅行商)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3160 Accepted: 1613 ...
- pygame 练习之 PIE game (以及简单图形训练)
简单的大饼游戏,掌握pygame中直线以及圆弧的画法,以及对输入的响应. import math import pygame, sys from pygame.locals import * pyga ...
- Pizza Pie Charts – 基于 Snap SVG 框架的响应式饼图
Pizza Pie Charts 是一个基于 Adobe 的 Snap SVG 框架的响应式饼图插件.它着重于集成 HTML 标记和 CSS,而不是 JavaScript 对象,当然Pizza Pie ...
- 【poj3122】 Pie
http://poj.org/problem?id=3122 (题目链接) 题意 给出N个pie的半径和F个friend,每个friend得到的pie必须一样,求每个人能得到的pie的最大大小. so ...
- tcpdump for android L 5.x with pie support
由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题. 最近由于要测试在Android L上的运行情况发现,当运行该可执行文件时,报如下错误: error: only po ...
- 分馅饼 Pie
Pie 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/C 题目: Problem Description ...
- Pie(二分POJ3122)
Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12985 Accepted: 4490 Special Ju ...
随机推荐
- Adobe登陆出现Access denied解决方法
当我安装好Adobe Acrobat Reader DC时,想要登陆Adobe账号时被Access denied. 一般说需要梯子,然而本人亲测这种方法不行(香港主机,全局模式下). 一个简单有效的方 ...
- groupadd - 建 立 新 群 组
总览 SYNOPSIS groupadd [-g gid [-o]] [-r] [-f] group 描述 DESCRIPTION groupadd 可 指 定 群 组 名 称 来 建 立 新 的 群 ...
- Distinct Values(贪心)
问题 D: Distinct Values 时间限制: 1 Sec 内存限制: 128 MB提交: 13 解决: 5[提交] [状态] [讨论版] [命题人:admin] 题目描述 Chiaki ...
- js学习笔记-字符串
1.需要注意的是,JavaScript 的字符串是不可变的(immutable),String 类定义的方法都不能改变字符串的内容.像 String.toUpperCase() 这样的方法,返回的是全 ...
- 面向对象OONo.3单元总结
一,JML语言 1)JML理论基础:JML是一类语言,用来描述一个方法或一个类的功能.以及这个类在实现这个功能时需要的条件.可能改变的全局变量.以及由于条件问题不能实现功能时这个方法或类的行为,具有明 ...
- cocos2dx 单张图片加密
cocos2dx 已经封装好读取加密的prv文件的方法,打开texturepacker,导入一张图片,在content protection中写入密钥,在texture format中选择prv格式 ...
- numpy的linspace使用详解
文档地址: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html Parameters(参数): start ...
- noip考前注意事项
明天就要离开学校,后天的现在Day1已经考完啦,临近考试了紧不紧张呢(不紧张才怪),那么考前我们应该注意点什么呢? 1.带好自己的证件,带好自己的证件,带好自己的证件,这很重要. 2.试机的时候一定要 ...
- codis 配置
#修改dashboard.toml: coordinator_name = "zookeeper" coordinator_addr = "192.168.56.101: ...
- C++输入密码不显示明文
之前有遇到需求说输入密码不显示明文,但同时会有一些其他问题,暂时没做,如今经过尝试可以实现,但是得先知道要输入的是密码.主要利用的getch()函数的不回显特点.需要注意的是这个函数不是标准函数,而且 ...