题目链接 >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. 修改docker0默认IP地址

    第一步:vim /etc/docker/daemon.json { "registry-mirrors": ["https://docker.mirrors.ustc.e ...

  2. Git_GitHub详解

    Git和Github详细教程 一  概述 说到Git和Github,前几天我们知道微软以75亿美元收购全球最大的代码托管和写作平台GitHub,而GitHub是全球最大的代码仓库,很多开发人员都将代码 ...

  3. oc之封装「可输入值」的UIStepper

    2017.07.17 18:10* 字数 66 阅读 644评论 0喜欢 5 oc之封装「可输入值」的UIStepper 最终效果 要求: Platform: iOS7.0+ Language: Ob ...

  4. 实分析p78 两个解释

    1. 是为了存在一个充分大的J,使得,当j大于J.会满足.x是满足能一致收敛到f(x)自变量取得集合,, 是为了允许有限个 前面的不成立,是对所有的k都成立,让k取很大,可以很小 2.是函数列收敛到f ...

  5. Volterra方程的不动点

  6. ES使用C#添加和更新文档

    ElasticSearch 使用C#添加和更新文档 这是ElasticSearch 2.4 版本系列的第四篇: 第一篇:ES1:Windows下安装ElasticSearch 第二篇:ES2:Elas ...

  7. 解决scrapy报错:ModuleNotFoundError: No module named 'win32api'

    ModuleNotFoundError: No module named 'win32api' 表示win32api未安装 解决办法: 下载对应python版本的win32api,并安装. 下载地址: ...

  8. asp.net mvc或者其他程序无法打开excel——解决方案,C#处理Excel文件

    问题描述:今天处理Excel时遇到一个问题,本地使用Microsoft.Jet.OLEDB.4.0处理,正常完成了需求, 上传到服务器后发生了异常,通过排查发现问题出现在对Excel文件的读取上,然后 ...

  9. async并发处理

  10. redux模块化demo

    store.js 在redux中 store 是唯一的. import {createStore} from 'redux'; import reducer from './reducer' // 引 ...