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问题的最小解 ...
随机推荐
- 电商指尖---(6)solrconfig.xml配置具体解释
solrconfig.xml配置文件主要定义SOLR理规则,包含索引数据的存放位置,更新,删除,查询的一些规则配置. 能够在tomcat的安装路径下找到这个文件C:\Program Files\Apa ...
- Paypal-Express Checkout快捷支付方式的android端开发心得(二)
一.前导 上一篇讲的不是非常好,这里再又一次讲一下. Paypal手机支付有2种形式: 1.Mobile Express Checkout,MEC,快捷支付 2.MPL 假设採用MEC支付方式,这样的 ...
- 它们的定义PropertyPlaceHolder无法完成更换任务
Spring默认PropertyPlaceholderConfigurer只能加载properties格风格简介,现在,我们需要能够从类的完整支持允许似hadoop格风格xml配置文件读取配置信息,并 ...
- ZOJ 3820 Building Fire Stations
题意: 树上找两个点 使得其它点到这两点随意一点的距离的最大值最小 思路: 最大值最小 想到二分 在二分的基础上判定这个最大值是否可能 怎样判定这个问题就是怎样选那两个点的问题 非常明显 我 ...
- C# - Dictionary join keys or join Values
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class P ...
- 数据库管理——安全管理——识别SQLServer中空密码或者弱密码的登录名
原文:数据库管理--安全管理--识别SQLServer中空密码或者弱密码的登录名 原文译自: http://www.mssqltips.com/sqlservertip/2775/identify-b ...
- Cocos2d-Java安装和配置跨平台游戏引擎以及相关的开发工具
假设认为博文图片不清晰.能够Ctrl+鼠标滚动缩放网页比例 Cocos2d-Java是什么? http://blog.csdn.net/touchsnow/article/details/387047 ...
- 移动端 new CustomEvent('input') 兼容问题
最近在 安卓自带浏览器 上发现 new CustomEvent('input') 不兼容 解决办法 (function () { if(!!window.CustomEvent) return; f ...
- 跳跃Java一些周期,双跳FOR周期
今天写的代码写在一个双层for周期,目前仍在使用Iterator,大致意思是假定在第二个周期在排位赛中给了整个双回路跳. 刚開始,直接使用break.巴拉巴拉的敲了一堆代码,信心满满的就直接执行.等到 ...
- IOS 数据库管理系统(SQLite)
嵌入式数据库 SQLite嵌入式数据库的优势 1.支持事件,你并不需要配置,无需安装,不需要管理员 2.支持部分脂肪SQL92 3.完整的数据库被存储在磁盘上的文件的顶部,相同的数据库文件可以在不同机 ...