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-UITableCell详情
iOS-UITableCell详情 表示UITableViewCell风格的常量有: UITableViewCellStyleDefault UITableViewCellStyleSubtitle ...
- 设置ViewController 数据源无法改变view
病情描述: viewController创建的时候勾选了xib,然后在显示的时候调用了如下语句: MTDetailDealViewController *detailController = [[MT ...
- 11.2 morning
noip模拟题day1——棋盘上的问题 day1模拟题 By FancyCoder总览(Overview)注意事项:共3道题目,时间2.5小时.Pascal选手允许使用math库和ansistring ...
- root密码忘记了要怎么搞
我是根据这几个教程做的,网址如下 http://www.pc6.com/infoview/Article_65979.html http://down.chinaz.com/server/201111 ...
- Asp.net Mvc4 基于Authorize实现的模块访问权限
在MVC中,我们可以通过在action或者controller上设置Authorize[Role="xxx"] 的方式来设置用户对action的访问权限.显然,这样并不能满足我们的 ...
- RESTful互联网框架
在我们日常接触的网络中,对于非程序员来说主要关注的就是在网上找到自己需要的资料,但是对于开发者来说,主要关注的就是将结构和页面,以及功能的分离,但是如何划分这个结构呢,或许我们知道的有MVC框架,甚至 ...
- javascript的框架演化
说起javascript不同的人或许有不同的看法,一些资深后台程序员在刚开始的时候根本没有把它当作是一门编程语言,但是随着后面js框架的出现,以及面向对象的程序设计,还有原型,闭包的不断使用,后台程序 ...
- sqlserver-事务处理
事务的概念:简单说就访问并可能更新数据库中各种数据项的一个程序执行单元,一旦开启事务,所有对数据的操作要么全部执行,要么全部都不执行.单条sql语句本身就是一个事务. 事务的属性: 事务是作为单个逻辑 ...
- corejava_chap02
//单行注释 --不能用在一行代码的中间/**/多行注释 --任何地方/** */文档注释 文档注释用在:package.class.member variables.member method. ...
- PHP 用户注册
注册页面 reg.html 负责收集用户填写的注册信息.教程里只列出关键的代码片段,完整的代码附在本节最后. 注册表单 <fieldset> <legend>用户注册</ ...