POJ 3311 Hie with the Pie floyd+状压DP
链接:http://poj.org/problem?id=3311
题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多少。
思路:首先用floyd找到全部点之间的最短路。然后用状态压缩,dp数组一定是二维的,假设是一维的话不能保证dp[i]->dp[j]一定是最短的。由于dp[i]记录的“当前位置”不一定是能使dp[j]最小的当前位置。所以dp[i][j]中,i表示的二进制下的当前已经经过的状态,j表示的是在当前状态下眼下所在的位置。
代码:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define eps 1e-8
#define INF 0x7fffffff
#define PI acos(-1.0)
#define seed 31//131,1313
#define maxn 15
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
int dp[1<<10][maxn];
int Pow[maxn];
int back[maxn];
int cost[maxn][maxn];
void init1()
{
Pow[0]=1;
for(int i=1; i<=10; i++)
Pow[i]=Pow[i-1]*2;
}
void init2()
{
for(int i=1; i<(1<<10); i++)
for(int j=0; j<=10; j++)
dp[i][j]=INF;
}
int floyd(int a[][maxn],int t)
{
for(int i=0; i<t; i++)
for(int j=0; j<t; j++)
for(int k=0; k<t; k++)
a[i][j]=min(a[i][k]+a[k][j],a[i][j]);
}
int main()
{
int T,x;
init1();
while(scanf("%d",&T))
{
init2();
if(!T)
break;
for(int i=0; i<T+1; i++)
for(int j=0; j<T+1; j++)
scanf("%d",&cost[i][j]);
floyd(cost,T+1); for(int i=0; i<T; i++)
dp[Pow[i]][i]=cost[0][i+1];
for(int i=0; i<T; i++)
back[i]=cost[i+1][0];
for(int i=0; i<T; i++)
for(int j=0; j<T; j++)
cost[i][j]=cost[i+1][j+1];
for(int i=0; i<(1<<T); i++)
{
if(i==1||i==2||i==4||i==8||i==16||i==32||i==64||i==128||i==256||i==512)
continue;
int ii=i;
int pos=0;
while(ii)
{
if(ii%2==1)
{
int t=i-Pow[pos];
for(int j=0; j<T; j++)
if(dp[t][j]!=INF&&dp[t][j]+cost[j][pos]<dp[i][pos])
dp[i][pos]=dp[t][j]+cost[j][pos];
}
ii>>=1;
pos++;
}
}
int ans=INF;
for(int i=0; i<T; i++)
{
if(dp[(1<<T)-1][i]!=INF&&dp[(1<<T)-1][i]+back[i]<ans)
ans=dp[(1<<T)-1][i]+back[i];
} printf("%d\n",ans);
}
return 0;
}
POJ 3311 Hie with the Pie floyd+状压DP的更多相关文章
- 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(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...
- POJ 3311 Hie with the Pie 【状压DP】
Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possi ...
- poj 3311 Hie with the Pie (状压dp) (Tsp问题)
这道题就是Tsp问题,稍微加了些改变 注意以下问题 (1)每个点可以经过多次,这里就可以用弗洛伊德初始化最短距离 (2)在循环中集合可以用S表示更清晰一些 (3)第一维为状态,第二维为在哪个点,不要写 ...
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...
- TZOJ 1937 Hie with the Pie(floyd+状压dp)
描述 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...
- POJ3311 Hie with the Pie 【状压dp/TSP问题】
题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total ...
- 【POJ3311】Hie with the Pie(状压DP,最短路)
题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...
- [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
主题连接: id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...
随机推荐
- 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)
职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...
- Python 获得Facebook用户有一个共同的兴趣Friends
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-13 @author: guaguastd @name: c ...
- 日积月累:weightSum和layout_weight属性合用
解说一:weightSum和layout_weight属性合用 android:weightSum属性:定义weight总和的最大值. 假设为指定该值,全部子视图的layout_weight属性的累加 ...
- Action、Category、Data、Extras知识具体解释
开头 Intent作为联系各Activity之间的纽带,其作用并不仅仅仅仅限于简单的数据传递.通过其自带的属性,事实上能够方便的完毕非常多较为复杂的操作.比如直接调用拨号功能.直接自己主动调用合适的程 ...
- 第一章 工欲善其事 其利润—Android SDK工具(2)
1.2设备管理工具-调试桥(ADB) 1.2.1ADB简单介绍 ADB全称是Android Debug Bridge,是Android SDK里自带的一个工具,用这个工具能够直接操作管理Android ...
- nginx源代码分析--读请求主体(1)
首先,读取请求体已进入HTTP要求11相,我们需要做的请求正文部分处理一些模块,所以这个模块需要注册功能在这个阶段,在阅读功能要求的身体ngx_http_read_client_request_bod ...
- Eclipse build launcher 3具体步骤
1. 下载launcher 3源代码 (需要FQ) git clone https://android.googlesource.com/platform/packages/apps/Launche ...
- 有关XCode6(iOS8)UITableViewCell与iOS7在UITableViewCell问题
简而言之: iOS6在cell的层次关系2层,但在iOS7层次结构成为3层,但在iOS8的SDK在UITableViewCell层次结构发生了变化2层. 如果它们是UITableViewCell加入到 ...
- ACM-DP最大连续子——hdu1231
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- MPC8313ERDB不新鲜pkg包裹,把文件放进Ramdisk
MPC8313ERDB不新鲜pkg包裹,把文件放进Ramdisk 经ltib编译器生成rootfs.ext2.gz.uboot它可以直接uboot采用.假设我们编写了相应的外部文件把Ramdisk往里 ...