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问题的最小解 ...
随机推荐
- 高清电视产业的关键词,4K过渡时期8K未来
有些不尽人意,归根结底在于,绝大多数厂商并没有把电视的性能作为突破口,相反,仅仅是在外观.设计上做起了文章.在部分厂商看来,要真正研发性能一流的智能电视须要更高的投入,但改变一下外形似乎也能获 ...
- ios至于理解锚
锚点ios出现在少数地方,多数用在动画. 今天看了一部电影,以上所有关于锚,两年前锚这个概念看cocos2d当被接触的基本概念,当时我没怎么看,今天看了,刚刚好学习. 阅读blog,它是关于锚,像: ...
- (DDD)仓储的思考
(DDD)仓储的思考 为什么需要仓储呢?领域对象(一般是聚合根)的被创建出来后的到最后持久化到数据库都需要跟数据库打交道,这样我们就需要一个类似数据库访问层的东西来管理领域对象.那是不是我们就可以设计 ...
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法(转)
在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出).Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是BlockingQ ...
- SharePoint管理中心来配置资源限制(大名单)
SharePoint管理中心来配置资源限制(大名单) 名单SharePoint核心.SharePoint一切的一切都是列表. 我可以说SharePoint内容为驱动的列表. 之前版本号的SharePo ...
- PC结束 Spark 二次开发 收到自己主动,并允许好友请求
本次Spark二次开发是为了客服模块的开发, 能让用户一旦点击该客服则直接自己主动加入好友.而客服放则需自己主动加入好友,不同弹出对话框进行允许,这方便的广大客服. 如今废话不多说,直接上代码. pa ...
- Windows下一个MySQL有些错误的解决方法
1.无论是什么提示.我们有一个直接看错误日志.由于它描述了最具体描述错误日志. 于MySQL安装文件夹中找到 my.ini简介 看日志保存路径 2. 我的错误是[ERROR] Fatal error: ...
- Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL
http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php- ...
- Cocos2d-x3.0 RenderTexture(一) 保存
.h #include "cocos2d.h" #include "cocos-ext.h" #include "ui/CocosGUI.h" ...
- Codeforces 490F. Treeland Tour 暴力+LIS
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...