题意:类似于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+最短路+状态压缩)的更多相关文章

  1. poj 3311 Hie with the Pie

    floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Me ...

  2. poj 3311 Hie with the Pie (TSP问题)

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 ...

  3. POJ 3311 Hie with the Pie(状压DP + Floyd)

    题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...

  4. POJ 3311 Hie with the Pie floyd+状压DP

    链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...

  5. poj 3311 Hie with the Pie dp+状压

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 ...

  6. POJ 3311 Hie with the Pie 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

  7. POJ 3311 Hie with the Pie(DP状态压缩+最短路径)

    题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...

  8. [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法

    主题连接:  id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...

  9. POJ 3311 Hie with the Pie:TSP(旅行商)【节点可多次经过】

    题目链接:http://poj.org/problem?id=3311 题意: 你在0号点(pizza店),要往1到n号节点送pizza. 每个节点可以重复经过. 给你一个(n+1)*(n+1)的邻接 ...

随机推荐

  1. In-System Debugger for 8051 Devices(ISD 8051单片机在线调试器)

    此文档包含了最新版本的说明及最近的更新特别是对 ISD51 的说明(用户手册没有此说明) Keil Software,Inc and Keil Elektronik GmbH保留所有此文件中涉及的信息 ...

  2. Unix/Linux环境C编程入门教程(15) BT5开发环境搭建

    1. Backtrack 是处于世界领先地位的渗透测试和信息安全审计发行版本.有着上百种预先安装好的工具软件,并确定能完美运行,Backtrack5 提供了一个强大的渗透测试平台--从Web hack ...

  3. 哥德尔,图灵和康托尔 part 2 停机问题

    图灵著名的停机问题对于软件开发者而已是非常熟悉的.下面简单描述停机问题: 假设给你一个计算机程序的源代码,也给你所有程序要用的数据,文件,硬盘,DVD等等,所有它需要处理的东西.你能告诉我程序最终是否 ...

  4. css样式实现字体删除线效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. Nginx+Tomcat7+Mencached负载均衡集群部署笔记

    Nginx+Tomcat+Memcached负载均衡集群服务搭建 操作系统:CentOS6.5 本文档主要解说,怎样在CentOS6.5下搭建Nginx+Tomcat+Memcached负载均衡集群s ...

  6. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  7. Ext.Net 使用总结之查询条件中的起始日期

    2.关于查询条件中起始日期的布局方式 首先上一张图,来展示一下我的查询条件的布局,如下: 大多数时候,我们的查询条件都是一个条件占一个格子,但也有不同的时候,如:查询条件是起始日期,则需要将这两个条件 ...

  8. ORACLE恢复误删除的对象(表、存储过程等)

    1.恢复存储过程 原理就是利用了oracle里所有的存储过程的源代码都是存在dba_source里,而drop某个存储过程的时候,oracle这里肯定要去dba_source里把相关的源代码给dele ...

  9. Git基本应用

    1.创建SSH Key $ cd ~/.ssh $ ssh-keygen -t rsa -C "your_email@example.com" 拷贝id_rsa.pub文件到Set ...

  10. iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见

    iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见 (2015-12-05 12:48:20)[编辑][删除] 转载▼     首先我们先明确一下问题: 1.因为UI是在主线 ...