题目链接 >http://poj.org/problem?id=3311<

题意:从0出发,经过所有点(点可以重复走)后回到0点,问最短路

思路分析:

  这题和普通的最短路不太一样,因为题目要求每个点都要走一遍。

  因此我们选择状压。

  用SPFA直接开始做,f[i][status]表示到达点i时,状态为status时的最短路,答案就是f[0][(1<<(n+1))-1]

  更新也和SPFA一模一样

/*By QiXingzhi*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define r read()
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
const int N = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar();
return x * w;
}
struct Pizza{
int u, sta;
};
int n;
int G[N][N],d[N][];
queue <Pizza> q;
inline void Debug(int status){
int tmp[];
memset(tmp,,sizeof(tmp));
int tot = ;
while(status > ){
tmp[++tot] = status % ;
status /= ;
}
for(int i = tot; i > ; --i){
printf("%d",tmp[i]);
}
}
inline void SPFA(){
// printf("u = %d status = ",u); Debug(status); printf(" d = %d\n",d[u][status]);
memset(d,0x3f,sizeof(d));
d[][] = ;
Pizza tmp; tmp.u = , tmp.sta = ;
q.push(tmp);
int u,status;
while(!q.empty()){
Pizza __t = q.front();
Pizza __next;
u = __t.u;
status = __t.sta;
q.pop();
for(int i = ; i <= n; ++i){
if(G[u][i] == ) continue;
if(d[u][status] + G[u][i] < d[i][status ^ ( << (i))]){
d[i][status ^ ( << (i))] = d[u][status] + G[u][i];
__next.u = i;
__next.sta = status ^ ( << (i));
q.push(__next);
}
}
} }
int main(){
// freopen(".in","r",stdin);
// freopen("debug.txt","w",stdout);
while(){
n = r;
if(n == ) break;
for(int i = ; i <= n; ++i){
for(int j = ; j <= n; ++j){
G[i][j] = r;
}
}
SPFA();
printf("%d\n", d[][(<<(n+))-]);
}
return ;
}

「POJ3311」Hie with the Pie的更多相关文章

  1. 【POJ3311】Hie with the Pie(状压DP,最短路)

    题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...

  2. POJ3311 Hie with the Pie 【状压dp/TSP问题】

    题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total ...

  3. poj3311 Hie with the Pie (状态压缩dp,旅行商)

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

  4. Hie with the Pie(poj3311)

    题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...

  5. Hie with the Pie

    Hie with the Pie poj-3311 题目大意:n+1个点,伪旅行商问题. 注释:n<=10. 想法:咳咳,第一道状压dp,下面我来介绍一下状压dp. 所谓dp,就是动态性决策规划 ...

  6. 零元学Expression Design 4 - Chapter 7 使用内建功能「Clone」来达成Path的影分身之术

    原文:零元学Expression Design 4 - Chapter 7 使用内建功能「Clone」来达成Path的影分身之术 本章所介绍的是便利且快速的内建工具Clone ? 本章所介绍的是便利且 ...

  7. 零元学Expression Blend 4 - Chapter 34 啊~!!我不要毛毛的感觉!-使用布局修整「UseLayoutRounding」

    原文:零元学Expression Blend 4 - Chapter 34 啊~!!我不要毛毛的感觉!-使用布局修整「UseLayoutRounding」 本章将介绍UseLayoutRounding ...

  8. 零元学Expression Blend 4 - Chapter 18 用实例了解互动控制项「CheckBox」II

    原文:零元学Expression Blend 4 - Chapter 18 用实例了解互动控制项「CheckBox」II 延续上一章的CheckBox教学,本章将以实作继续延伸更灵活的运用CheckB ...

  9. 零元学Expression Blend 4 - Chapter 17 用实例了解互动控制项「CheckBox」I

    原文:零元学Expression Blend 4 - Chapter 17 用实例了解互动控制项「CheckBox」I 本章将教大家如何运用CheckBox做实作上的变化:教你如何把CheckBox变 ...

随机推荐

  1. .net core2 笔记

    资源: https://github.com/aspnet/home https://github.com/dotnet/cli https://www.cnblogs.com/billyang/p/ ...

  2. Log4J.xml配置详解

    原文地址:https://blog.csdn.net/genyizha/article/details/74502812 Appender Appender:日志输出器,配置日志的输出级别.输出位置等 ...

  3. Truncated Power Method for Sparse Eigenvalue Problems

    目录 算法 k的选择 \(x\)的初始化 代码 抱歉,真的没怎么看懂,当然,估计和我现在没法静下心来好好看也有关系. 算法 想法非常非常简单吧,就是在原来幂法的基础上,每次迭代的时候再加个截断.当然, ...

  4. VUE工程上线首页加载慢问题优化

    使用webpack-bundle-analyzer工具 下面介绍几种压缩文件的方式. 1.vue-router懒加载 2.工程文件打包的时候不生成.map文件 3.gzip压缩 4.CDN 5.VUE ...

  5. 使用 Markdown编辑

    作用: 学习笔记,整理日志, 发布日记,杂文,所见所想 撰写发布技术文稿(代码支持) 撰写发布学术论文(LaTeX 公式支持) sublime text3插件 输入 Shift + Ctrl + P, ...

  6. Visual Studio 2010 Shortcuts

    Uploaded by ProNotion, updated on 11/28/2013 by jmb Platform: Windows/ English  PDF    Print   Hide ...

  7. 【Python3练习题 009】 打印出所有的“水仙花数”

    # [Python练习题 009] 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,# 其各位数字立方和等于该数本身.例如:153是一个“水仙花数”,# 因为153=1的三次方+5的三次方+ ...

  8. js数据放入缓存,需要再调用

    再贴代码之前先描述下,这个技术应用的场景:一个页面的http请求次数能少点就少,这样大大提高用户体验.所以再一个页面发起一个请求,把所有数据都拿到后储存在缓存里面,你想用的时候再调用出来,这个是非常好 ...

  9. 微信小程序开发的基本流程

    微信小程序开发的基本流程 一,微信小程序简介 1,微信小程序简称小程序,张小龙在微信公开课 Pro 上发布的小程序正式上线,时间是2017年1月9日. 2,微信小程序这个词可以分解为“微信”和“小程序 ...

  10. 【转帖】远程显示(操作) 服务器 GUI 程序(图形化界面) (基于 X11 Forwarding + Centos + MobaXterm)

    远程显示(操作) 服务器 GUI 程序(图形化界面) (基于 X11 Forwarding + Centos + MobaXterm) https://zhuanlan.zhihu.com/p/310 ...