POJ 3311 Hie with the Pie (BFS+最短路+状态压缩)
题意:类似于TSP问题,只是每个点可以走多次,求回到起点的最短距离(起点为点0)。
分析:状态压缩,先预处理各点之间的最短路,然后sum【i】【buff】表示在i点,状态为buff时所耗时。。。。。。。
所以把10 * 1024 种状态来一遍,取sum【0】【(1<<n)-1】的最小值
只是把状态压缩DP改成bfs+状态压缩了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#define INF 0x7FFFFFFF
using namespace std; int dist[11][11],sum[11][1 << 10];
struct node {
int x,buff;
} q[55555];
int head,tail,n,ans; void floyd() {
for(int i=0; i<=n; i++) {
for(int j=0; j<=n; j++) {
for(int k=0; k<=n; k++) {
if(dist[j][i] + dist[i][k] < dist[j][k]) dist[j][k] = dist[j][i] + dist[i][k];
}
}
}
} void bfs() {
ans = INF;
memset(sum,0,sizeof(sum));
head = 0;
tail = 0;
q[head].x = 0;
q[head++].buff = 0;
while(head != tail) {
node t = q[tail ++];
node tt;
if(t.x == 0 && t.buff == (1 << n) - 1) {
ans = min(ans,sum[t.x][t.buff]);
}
for(int i=0; i<=n; i++) {
if(t.x == i) continue;
if(i != 0) {
if(t.buff & (1 << (i-1))) tt.buff = t.buff;
else tt.buff = t.buff + (1 << (i-1));
} else tt.buff = t.buff;
//如果该点该状态已经访问过,而这次如果没有更优解,则剪了
if(sum[i][tt.buff] != 0 && sum[i][tt.buff] > sum[t.x][t.buff] + dist[t.x][i]) {
sum[i][tt.buff] = sum[t.x][t.buff] + dist[t.x][i];
tt.x = i;
q[head++] = tt;
//未访问则老样子
} else if(sum[i][tt.buff] == 0) {
sum[i][tt.buff] = sum[t.x][t.buff] + dist[t.x][i];
tt.x = i;
q[head ++] = tt;
}
}
}
}
int main() {
while(scanf("%d",&n) && n) {
for(int i=0; i<=n; i++)
for(int j=0; j<=n; j++) {
scanf("%d",&dist[i][j]);
}
floyd();
bfs();
printf("%d\n",ans);
}
return 0;
}
POJ 3311 Hie with the Pie (BFS+最短路+状态压缩)的更多相关文章
- 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 (TSP问题)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4491 Accepted: 2376 ...
- 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 floyd+状压DP
链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...
- poj 3311 Hie with the Pie dp+状压
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4671 Accepted: 2471 ...
- POJ 3311 Hie with the Pie 最短路+状压DP
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11243 Accepted: 5963 ...
- POJ 3311 Hie with the Pie(DP状态压缩+最短路径)
题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...
- [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
主题连接: id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...
- POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】
题目链接:http://poj.org/problem?id=3311 题意: 你在0号点(pizza店),要往1到n号节点送pizza. 每个节点可以重复经过. 给你一个(n+1)*(n+1)的邻接 ...
随机推荐
- 【Howie玩docker】-windows下玩docker
Windows下安装toolbox一直没成功,于是投机取巧,用虚拟机手工打造玩docker的方法. 步骤: 安装虚拟机,安装centos 在win下建立共享文件夹,假如是 f:/share 在cent ...
- vb listview 的常用操作
常用操作:获取当前行数和列数: MsgBox "行数:" & ListView1.ListItems.Count & "列数:" & L ...
- QT连接mysql中文显示问题
亲测OK! #vim /etc/mysql/my.cnf [mysqld]下面加入: default-character-set=utf8 重启mysql /etc/init.d/mysql rest ...
- intent传对象
intent还有一个很好用的地方,就是传输对象,但要注意的是这里的传输只是将对象复制了一份通过intent进行传递,并不能达到实时更新的效果,也就是这个对象等偏重于“读”.intent对这个对象有着严 ...
- [LeetCode][Python]14: Longest Common Prefix
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- HDU 4308 BFS Saving Princess claire_
原题直通车:HDU 4308 Saving Princess claire_ 分析: 两次BFS分别找出‘Y’.‘C’到达最近的‘P’的最小消耗.再算出‘Y’到‘C’的最小消耗,比较出最小值 代码: ...
- Android图片裁剪之自由裁剪
我的博客http://blog.csdn.net/dawn_moon 客户的需求都是非常怪的.我有时候在给客户做项目的时候就想骂客户是sb.可是请你相信我,等你有需求,自己变成客户的时候,给你做项目的 ...
- spring NotWritablePropertyException异常
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'userDao' of bea ...
- package.json配置项
以下示例列举了常用的地方,一些不常用的可以查看文档:文档地址 { //该模块名字 "name":"test" , //版本 "version" ...
- android入门——Activity(1)
结构图 mainfests文件夹下面有一个AndroidMainfest.xml文件,类似web开发中的web.xml文件负责整个项目的配置,每当我们新建一个activity,都要在这个文件中进行配置 ...