poj 2432 Around the world bfs+哈希
由于每个点的状态包含走过来的距离,所以要存二维的状态,但是状态总量太多,所以可以用哈希来搞。
那么就是bfs最短路,哈希记录状态了。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5e3+9;
int n,m;
int a[maxn];
struct
{
struct
{
int next,to;
}e[maxn*10];
int head[maxn],lon;
void clear()
{
memset(head,-1,sizeof(head));
lon=-1;
}
void add(int from,int to)
{
e[++lon].to=to;
e[lon].next=head[from];
head[from]=lon;
}
}edge; struct
{
int head[111111],lon;
struct
{
int t,dist,next,sum;
}data[1111111];
void clear()
{
memset(head,-1,sizeof(head));
lon=-1;
}
bool push(int t,int dist,int sum)
{
int key=(t+abs(dist)%360*n)%111111;
for(int k=head[key];k!=-1;k=data[k].next)
{
if(t==data[k].t&&dist==data[k].dist)
return false;
}
data[++lon].t=t;
data[lon].dist=dist;
data[lon].next=head[key];
data[lon].sum=sum;
head[key]=lon;
return true;
}
}hash; struct
{
int t,dist,sum;
}que[1111111];
int bfs()
{
hash.clear();
int front=1,end=0;
que[++end].t=1;
que[end].sum=que[end].dist=0;
hash.push(1,0,0); while(front<=end)
{
int t=que[front].t;
int dist=que[front].dist;
int sum=que[front++].sum;
for(int k=edge.head[t];k!=-1;k=edge.e[k].next)
{
int u=edge.e[k].to;
int c=(a[u]+360-a[t])%360;
int uc=(a[t]+360-a[u])%360;
int tmp;
if(c<uc) tmp=c;
else tmp=-uc;
if(u==1&&dist+tmp!=0) return sum+1;
if(hash.push(u,dist+tmp,sum+1))
{
que[++end].t=u;
que[end].dist=dist+tmp;
que[end].sum=sum+1;
}
}
}
return -1;
} int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF)
{
edge.clear();
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1,from,to;i<=m;i++)
{
scanf("%d %d",&from,&to);
edge.add(from,to);
edge.add(to,from);
}
cout<<bfs()<<endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5e3+9;
int n,m;
int a[maxn];
struct
{
struct
{
int next,to;
}e[maxn*10];
int head[maxn],lon;
void clear()
{
memset(head,-1,sizeof(head));
lon=-1;
}
void add(int from,int to)
{
e[++lon].to=to;
e[lon].next=head[from];
head[from]=lon;
}
}edge; struct
{
int head[111111],lon;
struct
{
int t,dist,next,sum;
}data[1111111];
void clear()
{
memset(head,-1,sizeof(head));
lon=-1;
}
bool push(int t,int dist,int sum)
{
int key=(t+abs(dist)%360*n)%111111;
for(int k=head[key];k!=-1;k=data[k].next)
{
if(t==data[k].t&&dist==data[k].dist)
return false;
}
data[++lon].t=t;
data[lon].dist=dist;
data[lon].next=head[key];
data[lon].sum=sum;
head[key]=lon;
return true;
}
}hash; struct
{
int t,dist,sum;
}que[1111111];
int bfs()
{
hash.clear();
int front=1,end=0;
que[++end].t=1;
que[end].sum=que[end].dist=0;
hash.push(1,0,0); while(front<=end)
{
int t=que[front].t;
int dist=que[front].dist;
int sum=que[front++].sum;
for(int k=edge.head[t];k!=-1;k=edge.e[k].next)
{
int u=edge.e[k].to;
int c=(a[u]+360-a[t])%360;
int uc=(a[t]+360-a[u])%360;
int tmp;
if(c<uc) tmp=c;
else tmp=-uc;
if(u==1&&dist+tmp!=0) return sum+1;
if(hash.push(u,dist+tmp,sum+1))
{
que[++end].t=u;
que[end].dist=dist+tmp;
que[end].sum=sum+1;
}
}
}
return -1;
} int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF)
{
edge.clear();
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1,from,to;i<=m;i++)
{
scanf("%d %d",&from,&to);
edge.add(from,to);
edge.add(to,from);
}
cout<<bfs()<<endl;
}
return 0;
}
#include
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=5e3+9;
int n,m;
int a[maxn];
struct
{
struct
{
int next,to;
}e[maxn*10];
int head[maxn],lon;
void clear()
{
memset(head,-1,sizeof(head));
lon=-1;
}
void add(int from,int to)
{
e[++lon].to=to;
e[lon].next=head[from];
head[from]=lon;
}
}edge; struct
{
int head[111111],lon;
struct
{
int t,dist,next,sum;
}data[1111111];
void clear()
{
memset(head,-1,sizeof(head));
lon=-1;
}
bool push(int t,int dist,int sum)
{
int key=(t+abs(dist)%360*n)%111111;
for(int k=head[key];k!=-1;k=data[k].next)
{
if(t==data[k].t&&dist==data[k].dist)
return false;
}
data[++lon].t=t;
data[lon].dist=dist;
data[lon].next=head[key];
data[lon].sum=sum;
head[key]=lon;
return true;
}
}hash; struct
{
int t,dist,sum;
}que[1111111];
int bfs()
{
hash.clear();
int front=1,end=0;
que[++end].t=1;
que[end].sum=que[end].dist=0;
hash.push(1,0,0); while(front<=end)
{
int t=que[front].t;
int dist=que[front].dist;
int sum=que[front++].sum;
for(int k=edge.head[t];k!=-1;k=edge.e[k].next)
{
int u=edge.e[k].to;
int c=(a[u]+360-a[t])%360;
int uc=(a[t]+360-a[u])%360;
int tmp;
if(c<uc) tmp=c;
else tmp=-uc;
if(u==1&&dist+tmp!=0) return sum+1;
if(hash.push(u,dist+tmp,sum+1))
{
que[++end].t=u;
que[end].dist=dist+tmp;
que[end].sum=sum+1;
}
}
}
return -1;
} int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d %d",&n,&m)!=EOF)
{
edge.clear();
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1,from,to;i<=m;i++)
{
scanf("%d %d",&from,&to);
edge.add(from,to);
edge.add(to,from);
}
cout<<bfs()<<endl;
}
return 0;
}
<iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=5e3+9;int n,m;int a[maxn];struct{ struct { int next,to; }e[maxn*10]; int head[maxn],lon; void clear() { memset(head,-1,sizeof(head)); lon=-1; } void add(int from,int to) { e[++lon].to=to; e[lon].next=head[from]; head[from]=lon; }}edge;struct{ int head[1111111],lon; struct { int t,dist,next,sum; }data[1111111]; void clear() { memset(head,-1,sizeof(head)); lon=-1; } bool push(int t,int dist,int sum) { int key=t+abs(dist)%360*n; for(int k=head[key];k!=-1;k=data[k].next) { if(t==data[k].t&&dist==data[k].dist) return false; } data[++lon].t=t; data[lon].dist=dist; data[lon].next=head[key]; data[lon].sum=sum; head[key]=lon; return true; }}hash;struct{ int t,dist,sum;}que[1111111];int bfs(){ hash.clear(); int front=1,end=0; que[++end].t=1; que[end].sum=que[end].dist=0; hash.push(1,0,0); while(front<=end) { int t=que[front].t; int dist=que[front].dist; int sum=que[front++].sum; for(int k=edge.head[t];k!=-1;k=edge.e[k].next) { int u=edge.e[k].to; int c=(a[u]+360-a[t])%360; int uc=(a[t]+360-a[u])%360; int tmp; if(c<uc) tmp=c; else tmp=-uc; if(u==1&&dist+tmp!=0) return sum+1; if(hash.push(u,dist+tmp,sum+1)) { que[++end].t=u; que[end].dist=dist+tmp; que[end].sum=sum+1; } } } return -1;}int main(){// freopen("in.txt","r",stdin); while(scanf("%d %d",&n,&m)!=EOF) { edge.clear(); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1,from,to;i<=m;i++) { scanf("%d %d",&from,&to); edge.add(from,to); edge.add(to,from); } cout<<bfs()<<endl; } return 0;}
poj 2432 Around the world bfs+哈希的更多相关文章
- POJ 2432
\(\mathbf{POJ\;2432}\)题解 题意 给出圆上的\(N\)个点,每个点有一个经度(大于\(0\)小于\(360\)):再给出\(M\)条双向边,保证边\(x y\)仅会沿圆上较短的弧 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- UVA 10651 Pebble Solitaire(bfs + 哈希判重(记忆化搜索?))
Problem A Pebble Solitaire Input: standard input Output: standard output Time Limit: 1 second Pebble ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- POJ 3669 Meteor Shower【BFS】
POJ 3669 去看流星雨,不料流星掉下来会砸毁上下左右中五个点.每个流星掉下的位置和时间都不同,求能否活命,如果能活命,最短的逃跑时间是多少? 思路:对流星雨排序,然后将地图的每个点的值设为该点最 ...
- POJ 1573 Robot Motion(BFS)
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12856 Accepted: 6240 Des ...
- [POJ 1635] Subway tree systems (树哈希)
题目链接:http://poj.org/problem?id=1635 题目大意:给你两棵树的dfs描述串,从根节点出发,0代表向深搜,1代表回溯. 我刚开始自己设计了哈希函数,不知道为什么有问题.. ...
- POJ 3126 Prime Path (BFS)
[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.b ...
随机推荐
- IOS-CGAffineTransformMake 矩阵变换 的运算原理
1.矩阵的基本知识: struct CGAffineTransform { CGFloat a, b, c, d; CGFloat tx, ty; }; CGAffineTransform C ...
- TCP/IP协议原理与应用笔记01:OSI网络参考模型
1.OSI参考模型 第7层应用层:直接对应用程序提供服务,应用程序可以变化,但要包括电子消息传输 第6层表示层:格式化数据,以便为应用程序提供通用接口.这可以包括加密服务 第5层会话层:在两个 ...
- 开始android博客分享
现在开始写博客,分享android开发中的心得.
- 什么是php命名空间
php命名空间是在5.3版本后加入的,命名空间反过来就是空间命名,在这里的空间命名就像window下的文件夹命名,命名空间用关键字namespace来定义.在这里用文件夹举三个例子,比如相对于test ...
- 使用Convert 类和Parse方法将字符串转换为数值类型
//用Parse方法将字符串转换为数值类型; long num=Int64.Parse(args[2]) //用别名为Int64c#类型long; long num=long.Parse(args[2 ...
- linux常用命令之ln
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同不的链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的 ...
- Uploadify 笔记分享 -- 2014年10月18日
最近要做一个项目,有个部分需要用到Uploadify,以前用过,但不是很懂,找了无数遍的中文文档,发现好多都是以前的,都不能用,一时间索性自己写了个笔记,随用随查 <form> <i ...
- 安卓学习之ListView和GridView
ListView 和 GridView是安卓中显示信息的两个很基本也最常用的控件.他们的用法很相似,但是他俩也是有区别的. ListView显示的数据会将他的item放在一行显示,而且根据内容给出it ...
- 异常练习一 throw
package 异常练习;class OutageroudleException extends RuntimeException{ OutageroudleException(){ } Outage ...
- 【BZOJ2648】【kd_tree】SJY摆棋子
Description 这天,SJY显得无聊.在家自己玩.在一个棋盘上,有N个黑色棋子.他每次要么放到棋盘上一个黑色棋子,要么放上一个白色棋子,如果是白色棋子,他会找出距离这个白色棋子最近的黑色棋 ...