题意:类似于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. J2SE知识点摘记(二十五)

    Set 1.5.1        概述 Java 中的Set和正好和数学上直观的集(set)的概念是相同的.Set最大的特性就是不允许在其中存放的元素是重复的.根据这个特点,我们就可以使用Set 这个 ...

  2. javascript将异步校验表单改写为同步表单

    同步表单校验的缺点 响应错误信息时,需要重新加载整个页面(虽然有缓存,客户端仍然需要通过http协议对比每个文件是否有更新,以保持文件最新) 服务器响应错误以后,用户之前所输入的信息全部丢失了,用户需 ...

  3. Android切换页面效果的实现一:WebView+ViewFlipper

    前言: 这两周在帮学校做一个新生入学用的“新里程”的项目,要做到页面切换阅读的效果,自己百度了下,找到普遍是使用WebView+ViewFlipper的实现方法,但这种方法不能满足我的要求,因为它很难 ...

  4. poj2350

    #include <stdio.h> #include <stdlib.h> int main() { ],tim,i; scanf("%d",&n ...

  5. Sql Server 2008 卸载重新安装失败的解决办法!(多次偿试,方法均有效!)

    Sql Server 2008 卸载重新安装失败的解决办法!(多次偿试,方法均有效!) 1.控制面板中卸载所有带sql server的程序. 2.在C盘C:\Program Files中sqlserv ...

  6. The Swift Programming Language--语言指南--协议

    Protocol(协议)用于统一方法和属性的名称,而不实现任何功能.协议能够被类,枚举,结构体实现,满足协议要求的类,枚举,结构体被称为协议的遵循者.   遵循者需要提供协议指定的成员,如属性,方法, ...

  7. java调试工具

    jps当前用户已启动的java进程信息,信息包括进程号和简短的进程command. jstat输出指定 jvm 实例的特定统计量:统计量:-class-compiler-gc-gccapacity-g ...

  8. jquery常用语句总结

    一.jquery中text val html attr的使用区别 html和innerHTMl是一样的,可以获得和设置指定元素如<p>中的html标签和文本如:设置值: $("p ...

  9. 【ActionBar的使用】

    在AS工程中使用ActionBar 简单实用: 1.功能清单文件中指定主题标签属性Theme.Holo或其子类 <application android :theme="@androi ...

  10. C++ this 指针

    类的(非静态)成员函数具有一个附加的隐含形参,即指向该类对象的一个指针.这个隐含形参命名为this,与调用成员函数的对象绑定在一起.成员函数不能定义this形参,而是由编译器隐含地定义.成员函数的函数 ...