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 ...
随机推荐
- hd - MFM/IDE 硬盘设备
描述 DESCRIPTION hd* 开头的设备是以裸模式(raw mode)访问MFM/IDE类型硬盘的块设备. 第一个IDE驱动控制器上的主盘(主设备号3)是 hda ;从盘是 hdb. 第二个I ...
- Linux增加sudo用户
1. root用户编辑文件/etc/sudoers vi /etc/sudoers 2. 按yy复制行root ALL=(ALL) ALL 按p粘贴,修改 “root” 为添加的用户 ...
- centos7-httpd虚拟主机
Apache虚拟主机: 一台WEB服务器发布单个网站会非常浪费资源,所以一台WEB服务器上会发布多个网站, 在一台服务器上发布多网站,也称之为部署多个虚拟主机,WEB虚拟主机配置方法有三种: 基于单I ...
- Bootstrap历练实例:堆叠的进度条
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Redis 和缓存技术
Redis 是什么?什么作用?优点和缺点? https://blog.csdn.net/weixin_42295141/article/details/81380633 Redis 的主要功能哨兵+复 ...
- HDU-1072-Nightmares
这题可以用dfs写,我们记忆化搜索. 我们定义一个step和time数组,分别表示走到这点的最小步数,time表示走到该点炸弹还剩多少时间. 递归边界一是,如果走到该点,时间等于0,我们就返回. 如果 ...
- jenkins 插件
- k8s的Pod控制器
pod的配置清单常见选项: apiVersion,kind,metadata,spec,status(只读) spec: containers: nodeSelector: nodeName: res ...
- mac Parallels Desttop 13 win7/win8 无法连接网络
把 “源” 从共享网络改为"Wi-Fi" 在mac 这边点击菜单栏windows图标,选配置(如果没有配置,点控制中心,在点控制中心的齿轮,)选 硬件 找到网络 解锁,上边第一行就 ...
- jQuery发送ajax请求实现跨域访问
Java代码的话,在返回响应之前调用如下代码中的allowCrossDomainAccess()方法: /** * 允许跨域访问 */ public void allowCrossDomainAcce ...