题意:

      给你一个地图,地图上有公交站点和路线,问你从起点到终点至少要换多少次公交路线。

思路:

      首先上面的题意说的和笼统,没说详细是因为这个题目叙述的很多,描述起来麻烦,

下面说思路,做这个题首先我们要把起点和终点的坐标求出来,每次点击地图我都是记录当前现则框的坐上角坐标,最后确定图之后再加上给的x,y转换后的实际位置,这样就的到了精准的位置,然后建图,题目让求的是换车次数,而题目给的是路径,所以我们要把每个路径都拆成任意边,比如 a -> b - > c 要拆成 a - b ,a - c ,b - c这三条,然后在起点和终点根据限制加进来,因为距离都是1可以最短路也可以广搜,(广搜速度会快点),我写的是最短路,这个无所谓,还有一个关键的地方就是hash车站地点,一开始我用的map果断超时了,因为map的操作是设计到排序的,所以超时了,(然后就没有去优化map,其实可以用vector,或者别的不设计到排序的容器,自己STL会的不是很多所以就没尝试去用)最后我是直接写了一个字典树,虽然有点麻烦,但没难度,所以就用字典树去hash名字吧,具体看代码。


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue> #define N_node 5500
#define N_edge 100000
#define INF 1000000000

using namespace
std; typedef struct
{
int
to ,next ,cost;
}
STAR; typedef struct
{
double
x ,y;
}
NODE; typedef struct Tree
{

Tree *next[26];
int
v;
}
Tree; Tree root;
STAR E[N_edge];
NODE node[N_node];
int
list[N_node] ,tot;
int
s_x[N_node];
double
dir[4][2] = {0 ,0 ,0 ,0.5 ,0.5 ,0 ,0.5 ,0.5}; void add(int a ,int b ,int c)
{

E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
E[++tot].to = a;
E[tot].cost = c;
E[tot].next = list[b];
list[b] = tot;
} double
Get_dis(NODE a ,NODE b)
{
double
x = (a.x - b.x) * (a.x - b.x);
double
y = (a.y - b.y) * (a.y - b.y);
return
sqrt(x + y);
}
NODE Get_se(char str[] ,double x ,double y)
{

NODE ans;
ans.x = ans.y = 0;
double
now = 10240;
for(int
i = 0 ;i < 8 ;i ++)
{

ans.x += now * dir[str[i] - '0'][0];
ans.y += now * dir[str[i] - '0'][1];
now /= 2;
}

ans.x += 10240 / pow(4.0 ,7.0) * x;
ans.y += 10240 / pow(4.0 ,7.0) * y;
return
ans;
} void
spfa(int s ,int n)
{
int
mark[N_node] = {0};
for(int
i = 0 ;i <= n ;i ++)
s_x[i] = INF;
mark[s] = 1 ,s_x[s] = 0;
queue<int>q;
q.push(s);
while(!
q.empty())
{
int
xin ,tou;
tou = q.front();
q.pop();
mark[tou] = 0;
for(int
k = list[tou] ;k ;k = E[k].next)
{

xin = E[k].to;
if(
s_x[xin] > s_x[tou] + E[k].cost)
{

s_x[xin] = s_x[tou] + E[k].cost;
if(!
mark[xin])
{

mark[xin] = 1;
q.push(xin);
}
}
}
}
return ;
} void
Buid_Tree(char *str ,int now)
{
int
len = strlen(str);
Tree *p = &root ,*q;
for(int
i = 0 ;i < len ;i ++)
{
int
id = str[i] - 'a';
if(
p -> next[id] == NULL)
{

q = (Tree *)malloc(sizeof(root));
//q -> v;
for(int j = 0 ;j < 26 ;j ++)
q -> next[j] = NULL;
p -> next[id] = q;
p = p -> next[id];
}
else

p = p -> next[id];
}

p -> v = now;
} int
Find(char *str)
{
int
len = strlen(str);
Tree *p = &root;
for(int
i = 0 ;i < len ;i ++)
{
int
id = str[i] - 'a';
p = p -> next[id];
}
return
p -> v;
} int main ()
{
int
t ,n ,m ,k ,i ,j;
double
x ,y;
char
str[50];
NODE s ,e;
scanf("%d" ,&t);
while(
t--)
{

scanf("%s %lf %lf" ,str ,&x ,&y);
s = Get_se(str ,x ,y);
scanf("%s %lf %lf" ,str ,&x ,&y);
e = Get_se(str ,x ,y);
int
nowid = 2;
scanf("%d" ,&n);
for(
i = 0 ;i < 26 ;i ++)
root.next[i] = NULL;
for(
i = 1 ;i <= n ;i ++)
{

scanf("%s %lf %lf" ,str ,&node[i+2].x ,&node[i+2].y);
Buid_Tree(str ,++nowid);
}

scanf("%d" ,&m);
char
tmp[33][22];
memset(list ,0 ,sizeof(list)) ,tot = 1;
while(
m--)
{

scanf("%d" ,&k);
for(
i = 1 ;i <= k ;i ++)
scanf("%s" ,tmp[i]);
for(
i = 1 ;i <= k ;i ++)
for(
j = i + 1 ;j <= k ;j ++)
add(Find(tmp[i]) ,Find(tmp[j]) ,1);
}
if(
Get_dis(s ,e) <= 2000)
{

puts("walk there");
continue;
}
for(
i = 3 ;i <= nowid ;i ++)
{
if(
Get_dis(s ,node[i]) <= 1000) add(1 ,i ,1);
if(
Get_dis(e ,node[i]) <= 1000) add(2 ,i ,1);
}

spfa(1 ,nowid);
s_x[2] == INF ? puts("take a taxi") : printf("%d\n" ,s_x[2] - 2);
}
return
0;
}

hdu2482 字典树+spfa的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. OpenGL中的简单坐标系初看+VAO/VBO/EBO

    你好,三角形 一: 关于坐标的问题 标准化设备坐标:输入的顶点数据就应该在标准化设备坐标范围里面即:x,y,z的值都在(-1-1)之间.在这个区间之外的坐标都会被丢弃. 1.1一旦顶点数据传入顶点着色 ...

  2. HDOJ-1540(线段树+较复杂的单点修改和区间查询)

    Tunnel Warfare HDOJ-1540 这题关于线段树的操作有一定的难度,需要较好的思维能力. 关于题目的详细解答已经在代码中体现了. #include<iostream> #i ...

  3. Selenium 4.0beta: Grid 工作原理

    Selenium 4.0 beta版已经发布,那么距离正式版已经不远了,在Selenium 4.0中变化比较大的就是Grid了,本文翻译了官方文档,重点介绍Grid 4的工作原理 Selenium G ...

  4. CVE-2020-1938 -Tomcat-AJP任意文件读取/包含

    为什么这个漏洞被称作 Ghostcat(幽灵猫)? 这个漏洞影响全版本默认配置下的 Tomcat(在我们发现此漏洞的时候,确认其影响 Tomcat 9/8/7/6 全版本,而年代过于久远的更早的版本未 ...

  5. solr 远程代码执行(CVE-2019-12409)

    Apache Solr 远程代码执行漏洞(CVE-2019-12409) 简介 Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.是apache的顶级开源项 ...

  6. MYSQL-SQLSERVER获取某个数据库的表记录数

    MYSQL: 1,可以使用MYSQL的系统表的记录数(亲测,有时候,会不准确,被坑了一把,如果还是想通过此方式实现查询表记录数,可以按照文章后的链接进行操作) use information_sche ...

  7. Python基础(1)——变量和数据类型[xiaoshun]

    目录 一.变量 1.概述 Variables are used to store information to be referenced(引用)and manipulated(操作) in a co ...

  8. 计算异质性H值(运用arcgis和Python进行区域分析)

    最近需要对ecognition分割结果进行统计分析,以此来进一步判断其分割结果中的欠分割和过分割对象,在看了一篇论文后,发现了可以用一个参数H来判断每个切割对象的异质性,由于此方法需要用到arcgis ...

  9. SHA算法摘要处理

    byte[] input="sha".getBytes();//待做消息摘要算法的原始信息,可以是任意字符串 MessageDigest sha=MessageDigest.get ...

  10. vue全家桶常用命名

    1,版本查看 node -vnpm -v2,修改NPM的缓存目录和全局目录路径 D盘node目录下创建两个目录,分别是node_cache和node_global,这是用来放安装过程的缓存文件以及最终 ...