POJ 3311 Hie with the Pie(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311
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
PS:
- //Floyd + 状态压缩DP
- //题意是有N个城市(1~N)和一个PIZZA店(0),要求一条回路。从0出发,又回到0,并且距离最短
- //也就是TSP(旅行商)问题,首先不难想到用FLOYD先求出随意2点的距离dis[i][j]
- //接着枚举全部状态,用11位二进制表示10个城市和pizza店,1表示经过,0表示没有经过
- //定义状态DP(i,j)表示在i状态下。到达城市j的最优值
题意解说什么的都去看:http://blog.csdn.net/chinaczy/article/details/5890768
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 17;
int d[maxn][maxn];
int dp[1<<maxn][maxn];
//dp[i][j]:i状态下,到达j城市的最短时间
#define INF 0x3f3f3f3f
int n;
void Floyd()
{
for(int k = 0; k <= n; k++)
{
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= n; j++)
{
if(d[i][j] > d[i][k]+d[k][j])
{
d[i][j] = d[i][k]+d[k][j];
}
}
}
}
}
int main()
{
while(scanf("%d",&n) && n)
{
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= n; j++)
{
scanf("%d",&d[i][j]);
}
}
Floyd();//跑出各点之间的最短距离 for(int i = 0; i <= (1<<n)-1; i++)
{
for(int j = 1; j <= n; j++)
{
if(i == (1<<(j-1)))//边界;
{
//状态i仅仅经过城市j则直接到达城市j
dp[i][j] = d[0][j];
}
else
{
dp[i][j] = INF;
}
}
}
for(int i = 0; i <= (1<<n)-1; i++)
{
for(int j = 1; j <= n; j++)
{
if(i & (1<<(j-1)))//状态i中已经经过城市j
{
for(int k = 1; k <= n; k++)//枚举中间城市找最小的距离
{
if(i&(1<<(k-1)) && j!=k)//枚举不是城市j的其它城市作为中间城市
{
//i^(1<<(j-1))表示未到达城市j的状态(使表示j这位的二进制变为0,注意第二层for的if())
dp[i][j] = min(dp[i][j],dp[i^(1<<(j-1))][k]+d[k][j]);
}
}
}
}
}
int ans_min = dp[(1<<n)-1][1]+d[1][0];
for(int i = 2; i <= n; i++)
{
if(ans_min > dp[(1<<n)-1][i]+d[i][0])
{
ans_min = dp[(1<<n)-1][i]+d[i][0];
}
}
printf("%d\n",ans_min);
}
return 0;
}
POJ 3311 Hie with the Pie(状压DP + Floyd)的更多相关文章
- POJ 3311 Hie with the Pie (状压DP)
dp[i][j][k] i代表此层用的状态序号 j上一层用的状态序号 k是层数&1(滚动数组) 标准流程 先预处理出所有合法数据存在status里 然后独立处理第一层 然后根据前一层的max推 ...
- 【鸽】poj3311 Hie with the Pie[状压DP+Floyd]
题解网上一搜一大坨的,不用复述了吧. 只是觉得网上dp方程没多大问题,但是状态的表示含义模糊.不同于正常哈密顿路径求解,状态表示应当改一下. 首先定义一次移动为从一个点经过若干个点到达另一个点,则$f ...
- East Central North America 2006 Hie with the Pie /// 状压dp oj22470
题目大意: 输入n,有n个地方(1~n)需要送pizza pizza点为0点 接下来n+1行每行n+1个值 表示 i 到 j 的路径长度 输出从0点到各点送pizza最后回到0点的最短路(点可重复走) ...
- poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp
题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间 ...
- POJ 3311 Hie with the Pie (状压DP)
题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORL ...
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...
- POJ 1321 棋盘问题(DFS & 状压DP)
用DFS写当然很简单了,8!的复杂度,16MS搞定. 在Discuss里看到有同学用状态压缩DP来写,就学习了一下,果然很精妙呀. 状态转移分两种,当前行不加棋子,和加棋子.dp[i][j]中,i代表 ...
- POJ:1185-炮兵阵地(状压dp入门)
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组 ...
- poj 2404 中国邮递员问题 欧拉回路判定+状压dp
/* 状压dp 邮递员问题:求经过任意点出发经过每一条边一次并回到原点. 解法:1.如果是欧拉回路那么就是所有的边的总和. 2.一般的解法,找出所有的奇度顶点,任意两个顶点匹配,即最小完美匹配,可用状 ...
随机推荐
- BarTender打印出来的条码与设计的不同如何处理
今日有用户在使用BarTender设计打印条码时发现自己设计出来的条码与打印显示的条码有不一样的地方,也就是BarTender模板上的条码有显示警戒栏,但打印的条码警戒栏却没了,这一问题要如何解决呢? ...
- base_local_planner vs. dwa_planner
http://answers.ros.org/question/10718/dwa_planner-vs-base_local_planner/ The dwa_local_planner suppo ...
- 如何把UIView转成UIImage,解决模糊失真问题
最近工作中,遇到一个需求,需要把一个UIView对象转成UIImage对象显示.经过网络搜索,找到如下答案: ? 1 2 3 4 5 6 7 8 -(UIImage*)convertViewToIma ...
- [jQuery] 自做 jQuery Plugin - Part 1
有時候寫 jQuery 時,常會發現一些簡單的效果可以重複利用.只是每次用 Copy & Paste 大法似乎不是件好事,有沒有什麼方法可以讓我們把這些效果用到其他地方呢? 沒錯,就是用 jQ ...
- [AngularJS系列(4)] 那伤不起的provider们啊~ (Provider, Value, Constant, Service, Factory, Decorator)(转)
用AngularJS做项目,但凡用过什么service啊,factory啊,provider啊,开始的时候晕没晕?!晕没晕?!感觉干的事儿都差不多啊,到底用哪个啊?!别告诉我你们几个就是为了跟我炫耀兄 ...
- uglifyjs入门接触
一.背景 今天在看<锋利的jQuery>文时,突然看到Uglifyjs压缩工具,感觉值得一试(玩),所以网上稍微搜了一下资料,简单的运用了一下,发现入门非常简单,当然网上有很多在线压缩工具 ...
- 【熊猫】POS销售
select a.itemcode,b.itemname,b.spec,b.unit,b.rprice,sum(a.rqty) rqtyfrom tm_possale_detail a,sys_ite ...
- [Tree]Count Complete Tree Nodes
Total Accepted: 22338 Total Submissions: 97234 Difficulty: Medium Given a complete binary tree, coun ...
- 简单的JQuery top返回顶部
一个最简单的JQuery Top返回的代码,Mark一下: HTML如下: <div id="backtop"> <a href="javascript ...
- android.view.InflateException: Binary XML file line #7: Error inflating class(OOM)
由于页面含有ImageView引起的内存溢出. 作如下处理:在OnDestroy中 Drawable d = imageView.getDrawable(); if (d != null) d.set ...