Hie 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

题意:求从点0出发经过其他所有点再回到0的最短路径

分析:很经典的状压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的更多相关文章

  1. POJ3311Hie with the Pie(floyd传递+DP,状态压缩)

    问题 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...

  2. [No0000A2]“原始印欧语”(PIE)听起来是什么样子?

    "Faux Amis"节目中经常提到"原始印欧语"(PIE)——"Proto-Indo-European". 我们说过,英语,法语中的&qu ...

  3. poj3311 Hie with the Pie (状态压缩dp,旅行商)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3160   Accepted: 1613 ...

  4. pygame 练习之 PIE game (以及简单图形训练)

    简单的大饼游戏,掌握pygame中直线以及圆弧的画法,以及对输入的响应. import math import pygame, sys from pygame.locals import * pyga ...

  5. Pizza Pie Charts – 基于 Snap SVG 框架的响应式饼图

    Pizza Pie Charts 是一个基于 Adobe 的 Snap SVG 框架的响应式饼图插件.它着重于集成 HTML 标记和 CSS,而不是 JavaScript 对象,当然Pizza Pie ...

  6. 【poj3122】 Pie

    http://poj.org/problem?id=3122 (题目链接) 题意 给出N个pie的半径和F个friend,每个friend得到的pie必须一样,求每个人能得到的pie的最大大小. so ...

  7. tcpdump for android L 5.x with pie support

    由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题. 最近由于要测试在Android L上的运行情况发现,当运行该可执行文件时,报如下错误: error: only po ...

  8. 分馅饼 Pie

    Pie 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/C 题目: Problem Description ...

  9. Pie(二分POJ3122)

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12985   Accepted: 4490   Special Ju ...

随机推荐

  1. [OpenMP] 并行计算入门

    OpenMP并行计算入门 个人理解 OpenMP是一种通过共享内存并行系统的多处理器程序设计的编译处理方案,通过预编译指令告诉编译器哪些代码块需要被并行化,通过拷贝代码块实现并行程序.对于循环的并行化 ...

  2. 队列的add与offer的区别

    两个方法都表示往队列里添加元素 但是当出现异常时,add方法抛出异常 而offer则返回的是false,就是啥事也没有,也不抛异常,也没有添加成功!

  3. selenium--iframe

    前戏 很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug也可以看到,就是定位不到,这种情况很有可能是frame在搞鬼. 进入到iframe <h ...

  4. javaweb基础(24)_jsp一般的标签开发

    一.标签技术的API 1.1.标签技术的API类继承关系 二.标签API简单介绍 2.1.JspTag接口 JspTag接口是所有自定义标签的父接口,它是JSP2.0中新定义的一个标记接口,没有任何属 ...

  5. Html5的等学习

    看了w3c感觉是说明文档,没有详细的说明,然后就去看其他的 html5其实就是在html的基础上做了一些改变,感觉html5的推广还是需要时间的,因为习惯问题,虽然html5有很多很方便的标签如art ...

  6. Mybatis学习记录(1)

    1.Mybatis介绍     Mybatis是apache的一个开源项目iBatis,Mybatis是一个优秀的持久层框架,他对jdbc的操作数据库的过程进行封装,使开发者只需要关注sql本身,不需 ...

  7. Maven:项目结构

    目录结构图: project        |- src            |- main   //工程源代码目录                |- java        //工程java源代 ...

  8. 使用CAShapeLayer实现复杂的View的遮罩效果

    一.案例演示 最近在整理一个聊天的项目的时候,发送图片的时候,会有一个三角的指向效果,指向这张图片的发送者.服务端返回给我们的图片只是一张矩形的图片,我们如何把一张矩形的图片或者View,加上一层自定 ...

  9. javascript设计模式(张容铭)学习笔记 - 外观模式绑定事件

    有一个需求要为document对象绑定click事件来是想隐藏提示框的交互功能,于是小白写了如下代码: document.onclick = function(e) { e.preventDefaul ...

  10. matplotlib subplot 子图

    总括 MATLAB和pyplot有当前的图形(figure)和当前的轴(axes)的概念,所有的作图命令都是对当前的对象作用.可以通过gca()获得当前的axes(轴),通过gcf()获得当前的图形( ...