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 ...
随机推荐
- [课堂总结]C++课堂总结(二)
近期的面向对象程序设计的不容易记忆或者理解的东西进行一个总结,以后忘记了可以常来看下,C++是个很重要的东西,很多领域都用得到,加油,特种兵! 浅拷贝构造.深拷贝构造 浅拷贝构造是系统默认的拷贝构造函 ...
- Maven settings.xml配置详解
首先:Maven中央仓库的搜索全部公共jar包的地址是,http://search.maven.org/ ===Maven基础-默认中央仓库============================== ...
- Forbidden You don't have permission to access /phpStudyTest/application/index/controller/Index.php on this server.
发生情况:将thinkPHP从官网上下了 http://thinkphp.cn 然后安装了phpstudy和PHPstorm,并将thinkPHP解压到www路径下 在用PHPstorm打开 thi ...
- Java中的后台线程和join方法
/*守护线程(后台线程):在一个进程中如果只剩下 了守护线程,那么守护线程也会死亡. 需求: 模拟QQ下载更新包. 一个线程默认都不是守护线程. */ public class Demo extend ...
- C++ 学习笔记 开篇
从大一开始学习C语言,大学期间做了许多嵌入式的开发项目,毕业后从事嵌入式开发工作主要的开发语言也是C语言.虽然期间断断续续的学习过C++,做过QT.C#上位机但也只是在其他语言的外壳下使用C在开发,始 ...
- C#基础-循环语句
while语句 int i = 1,sum=0; while (i <= 100) { sum += i; i++; } Console.WriteLine(sum); do···while语句 ...
- linux关于任务计划
1.一次性任务计划:at 1)添加 在18:16时候重启服务器 at 18:16 >at init 6 >at ctrl+d2)查看 atq 1 Mon Aug 20 21:09:00 2 ...
- 使用kickstart + pxe 部署无人值守安装
1.作为中小公司的运维,经常会遇到一些机械式的重复工作,例如:有时公司同时上线几十甚至上百台服务器,而且需要我们在短时间内完成系统安装. 常规的安装系统方法: 光盘安装系统:一个服务器DVD内置光驱百 ...
- 如何使用 HTML5 的picture元素处理响应式图片
来自: http://www.w3cplus.com/html5/quick-tip-how-to-use-html5-picture-for-responsive-images.html 图片在响应 ...
- Tcl/Cmds