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 一开始想通过终点值双向查找,从最高位开始依次递减或递增,每次找到最接近终点值的素数,后来发现这样找,即使找到,也可能不是最短路径, 而且代码实现起来特别麻烦,后来 ...
随机推荐
- SHELL打印两个日期之间的日期
SHELL打印两个日期之间的日期 [root@umout shell]# cat date_to_date.sh THIS_PATH=$(cd `dirname $0`;) cd $THIS_PATH ...
- SQL Server常见的操作符
常见的操作符:Sort.Hash Match(聚合).Filter.Compute Scalar等 一:Sort select Shelf from Production.ProductInvento ...
- 【Leetcode | 5】求和问题
一.1两数之和 二.15三数之和 C++ Soution 1: class Solution { public: vector<vector<int>> threeSum(ve ...
- 最新版谷歌浏览器的Flash设置已经不能保存了?
解决方法:先去chrome实验室界面chrome://flags/#enable-ephemeral-flash-permission选择取消Disabled.取消该实验室选项. 然后去chrome: ...
- TFS: 解决The build agent error - the session for this agent already exists
来源:http://ericphan.net/blog/2016/6/10/solving-the-tfs-build-agent-error-the-session-for-this-agent-a ...
- hbase启动后HMaster进程自动关闭
1.情况描述如题所示,hbase启动以后,HMaster进程启动了,几秒钟以后自动关闭,但是HRegionServer进程正常运行: 原因是,hdfs的默认端口号是8020,而我core-site.x ...
- [转] Javascript 原型链
1. 类 在C或者Java里,int a;定义了一个int类型的变量a.其中int是类型的名字,a是具体的变量. Javascript 模仿自 Java, 有一部分面向对象编程的部分.在面向对象的编程 ...
- .net core 发布后提示Start error
纪录篇: 发布Core版本的项目后一直提示error,通过网络查询资料后确认梳理问题的逻辑 1.验证环境是否支持,开发环境及server环境 参考:https://docs.micr ...
- [arc066f]Contest with Drinks Hard
题目大意: 有一些物品,每个买了有代价. 如果存在一个极大区间[l,r]内的物品都被买了,这个区间长度为k,可以获得的收益是k*(k+1)/2. 现在若干次询问,每次问假如修改了某个物品的价格,最大收 ...
- log4j.xml 报告集成
等级: trace< debug<info<warn<error<fatal trace 追踪 deug: eclipse, Log4j配置 [1]从零开始 a). ...