「POJ3311」Hie with the Pie
题目链接 >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的更多相关文章
- 【POJ3311】Hie with the Pie(状压DP,最短路)
题意: 思路:状压DP入门题 #include<cstdio> #include<cstdlib> #include<algorithm> #include< ...
- POJ3311 Hie with the Pie 【状压dp/TSP问题】
题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total ...
- poj3311 Hie with the Pie (状态压缩dp,旅行商)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3160 Accepted: 1613 ...
- Hie with the Pie(poj3311)
题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...
- Hie with the Pie
Hie with the Pie poj-3311 题目大意:n+1个点,伪旅行商问题. 注释:n<=10. 想法:咳咳,第一道状压dp,下面我来介绍一下状压dp. 所谓dp,就是动态性决策规划 ...
- 零元学Expression Design 4 - Chapter 7 使用内建功能「Clone」来达成Path的影分身之术
原文:零元学Expression Design 4 - Chapter 7 使用内建功能「Clone」来达成Path的影分身之术 本章所介绍的是便利且快速的内建工具Clone ? 本章所介绍的是便利且 ...
- 零元学Expression Blend 4 - Chapter 34 啊~!!我不要毛毛的感觉!-使用布局修整「UseLayoutRounding」
原文:零元学Expression Blend 4 - Chapter 34 啊~!!我不要毛毛的感觉!-使用布局修整「UseLayoutRounding」 本章将介绍UseLayoutRounding ...
- 零元学Expression Blend 4 - Chapter 18 用实例了解互动控制项「CheckBox」II
原文:零元学Expression Blend 4 - Chapter 18 用实例了解互动控制项「CheckBox」II 延续上一章的CheckBox教学,本章将以实作继续延伸更灵活的运用CheckB ...
- 零元学Expression Blend 4 - Chapter 17 用实例了解互动控制项「CheckBox」I
原文:零元学Expression Blend 4 - Chapter 17 用实例了解互动控制项「CheckBox」I 本章将教大家如何运用CheckBox做实作上的变化:教你如何把CheckBox变 ...
随机推荐
- Django 的路由层 视图层 模板层
--------------------------------------------------------------通过苦难,走向欢乐.——贝多芬 Django-2的路由层(URLconf) ...
- JDK8-新特性-附demo
import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Base64; impor ...
- Jmeter监控服务器笔记
Jmeter监控服务器-CPU,Memory,Disk,Network性能指标 本文主要说一下如何通过JMeter插件来监控服务器CPU.内存.磁盘.网络等相关资源. 一.下载 首先进入网址https ...
- python中$和@基础笔记
python 2.4以后,增加了@符号修饰函数对函数进行修饰,python3.0/2.6又增加了对类的修饰. $ 在正则表达式中,匹配一个字符串的末尾.(参考http://www.runoob.com ...
- IDEA 各版本在线激活(激活码)
lan yu 大佬的授权又被封杀了,还好我收藏了一些其他的服务器地址. 在线授权服务器 https://jetlicense.nss.im/ 授权代码 K03CHKJCFT-eyJsaWNlbnNlS ...
- Oracle循环
--无条件循环 declare v_num ; begin loop dbms_output.put_line(v_num); v_num:; ; end loop; end; --有条件循环 dec ...
- MongoDB操作(1)—MongoDB java驱动核心层次结构及操作流程
MongoDB之java驱动学习 预备: 本地运行MongoDB采用默认端口20717: 安装MongoDB驱动: 以下关键步骤. 核心层次结构或步骤: 创建连接池:MongoClient实例. 对于 ...
- Hbase存储模式
以key.value的结构存储数据; (table,rowkey,family,colum,timestamp)构成数据的key,value存储数据
- linux php7 安装redis扩展
1,下载redis扩展地址:https://pecl.php.net/package/redis 选择你需要的版本 上传redis-3.1.3.tar.gz到/usr/local/src目录 cd / ...
- django migrate报错(提前删除表等)
python3 manage.py makemigrations python3 manage.py migrate ##报错 改为##更改migrates的状态 python3 manage.py ...