Prime Path[POJ3126] [SPFA/BFS]
描述
孤单的zydsg又一次孤单的度过了520,不过下一次不会再这样了。zydsg要做些改变,他想去和素数小姐姐约会。
所有的路口都被标号为了一个4位素数,zydsg现在的位置和素数小姐姐的家也是这样,如果两个路口间只差1个数字,则有一条路连通两个路口。(例如1033和1073间有一条路连接)
现在,你知道了zydsg的位置和素数小姐姐的家,问最少zydsg要走多少条路才能见到素数小姐姐。例如:如果zydsg在1033,素数小姐姐的家在8179,最少要走6条街,走法为:
1033
1733
3733
3739
3779
8779
8179
其次,在每一组输入中,都包含两个数字a和b,代表zydsg的位置和素数小姐姐家的位置。
其中,a和b都是四位数,而且不含前导0。
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
分析
首先我们要筛素数,接下来
方法一:
两个“相邻的”素数连边,每次从开头向终点跑SPFA
方法二:
按个十百千位向四周扩散,BFS
代码(SPFA)
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define RG register int
#define rep(i,a,b) for(RG i=a;i<=b;++i)
#define per(i,a,b) for(RG i=a;i>=b;--i)
#define ll long long
#define inf (1<<29)
#define maxn 10005
#define lim 10002
#define maxm 1500005
#define add(x,y) e[++ct]=(E){y,head[x]},head[x]=ct
using namespace std;
int n,m,cnt,ct;
int isp[maxn],p[maxn],head[maxn],dis[maxn],vis[maxn],id[maxn];
struct E{
int v,next;
}e[maxm];
inline int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} inline int judge(int x,int y)
{
int sum=;
while(x) {if(x%!=y%) sum++;x/=,y/=;}
return sum==;
} void pre()
{
rep(i,,lim)
{
if(!isp[i]) p[++cnt]=i,id[i]=cnt;
for(RG j=;j<=cnt&&p[j]*i<=lim;j++)
{
isp[i*p[j]]=;
if(!(i%p[j])) break;
}
}
rep(i,,cnt) rep(j,i+,cnt) if(judge(p[i],p[j])) add(i,j),add(j,i);
} int SPFA(int S,int T)
{
memset(dis,,sizeof(dis));dis[S]=;
queue<int> que;que.push(S);
RG u,v;
while(!que.empty())
{
u=que.front(),que.pop(),vis[u]=;
for(RG i=head[u];i;i=e[i].next)
{
v=e[i].v;
if(dis[v]>dis[u]+){
dis[v]=dis[u]+;
if(!vis[v]) vis[v]=,que.push(v);
}
}
}
return dis[T];
} int main()
{
int Tim=read();
pre();
while(Tim--)
{
scanf("%d%d",&n,&m);
printf("%d\n",SPFA(id[n],id[m]));
}
return ;
}
Prime Path[POJ3126] [SPFA/BFS]的更多相关文章
- poj3126 Prime Path 广搜bfs
题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...
- 素数路径Prime Path POJ-3126 素数,BFS
题目链接:Prime Path 题目大意 从一个四位素数m开始,每次只允许变动一位数字使其变成另一个四位素数.求最终把m变成n所需的最少次数. 思路 BFS.搜索的时候,最低位为0,2,4,6,8可以 ...
- Prime Path(素数筛选+bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9519 Accepted: 5458 Description The m ...
- Prime Path POJ-3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- POJ 3126:Prime Path(素数+BFS)
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
- POJ 3126 - Prime Path - [线性筛+BFS]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
- POJ 3126 Prime Path (素数+BFS)
题意:给两个四位素数a和b,求从a变换到b的最少次数,每次变换只能变换一个数字并且变换的过程必须也是素数. 思路:先打表求出四位长度的所有素数,然后利用BFS求解.从a状态入队,然后从个位往千位的顺序 ...
- POJ3126 Prime Path (bfs+素数判断)
POJ3126 Prime Path 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
随机推荐
- 目标检测中的mAP
一.IOU的概念 交集和并集的比例(所谓的交集和并集,都是预测框和实际框的集合关系).如图: 二.Precision(准确率)和Recall(召回率)的概念 对于二分类问题,可将样例根据其真实类别和预 ...
- Windows 添加永久静态路由
route add -p 10.10.0.0 mask 255.255.0.0 10.10.6.1 -p 参数: p 即 persistent 的意思 -p 表示将路由表项永久加入系统注册表
- [转] Immutable 常用API简介
本文主要整理了Immutable.js常用API的使用. Immutable 是什么? 关于Immutable的定义,官方文档是这样说的: Immutable data encourages pure ...
- luogu 1471
题意: 蒟蒻HansBug在一本数学书里面发现了一个神奇的数列,包含N个实数.他想算算这个数列的平均数和方差. 操作1:1 x y k ,表示将第x到第y项每项加上k,k为一实数. 操作2:2 x y ...
- jenkins(4): jenkins 插件
1. jenkins插件下载镜像加速 jenkins插件清华大学镜像地址 https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-ce ...
- fillder--信息面板展示serverIP
1.Ctrl+R打开面板 2.如上图的位置,加上一句后,重启Fillder即可 FiddlerObject.UI.lvSessions.AddBoundColumn(, "X-HostIP& ...
- spring cloud (三、服务提供者demo_provider)
spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) 创建一个服务提供者注册到服务注册中心,跟前一个案例一样创建一个s ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第四集之安装Linux】
1,确保Linux镜像的路径存在 2,启动 3,在真实机情况下,进入BIOS修改安装操作系统的路径[记住:虚拟机不需要这一步.] 如果是真实机安装Linux,默认是从硬盘中安装,而不是从光盘.这就需要 ...
- 使用sparksql往kafka推送数据
一.相关配置参数 1.同级目录resource文件夹下配置 brokers_list=kafkaxxx02broker01:9092,kafkaxxx02broker02:9092,kafkaxxx0 ...
- JavaEE 之 WebService
1.WebService a.定义:WebService是一种跨编程语言和跨操作系统平台的远程调用技术 b.三大技术: XML+XSD,SOAP,WSDL c.SOAP协议 = HTTP协议 + XM ...