hdu2482 字典树+spfa
题意:
给你一个地图,地图上有公交站点和路线,问你从起点到终点至少要换多少次公交路线。
思路:
首先上面的题意说的和笼统,没说详细是因为这个题目叙述的很多,描述起来麻烦,
下面说思路,做这个题首先我们要把起点和终点的坐标求出来,每次点击地图我都是记录当前现则框的坐上角坐标,最后确定图之后再加上给的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的更多相关文章
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
- 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)
萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 字典树 - A Poet Computer
The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...
- trie字典树详解及应用
原文链接 http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用 一.知识简介 ...
- HDU1671 字典树
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- *HDU1251 字典树
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
随机推荐
- SpringCloud组件
1.Hystrix 1.1.简介 Hystrix,英文意思是豪猪,全身是刺,看起来就不好惹,是一种保护机制. Hystrix也是Netflix公司的一款组件. 主页:https://github.co ...
- Linux ctrl+z挂起的进程恢复与杀死
Linux系统下,不小心按了ctrl+z命令后,退出了当前进程的执行界面,程序没有结束,只是被挂起了.通过ps命令可以查看进程信息,这里不做详细介绍,可通过jobs命令查看被挂起的进程号 #jobs ...
- 剑指 Offer 12. 矩阵中的路径 + 递归 + 深搜 + 字符串问题
剑指 Offer 12. 矩阵中的路径 题目链接 题目类似于迷宫的搜索. 需要注意的是,需要首先判断起始搜索的位置,可能有多个起点,都需要一一尝试. 每轮迭代的时候记得将是否遍历标记数组还原为未遍历的 ...
- PAT-1102(Invert a Binary Tree)+二叉树的镜像+层次遍历+中序遍历+已知树的结构构树
Invert a Binary Tree pat-1102 import java.util.Arrays; import java.util.Queue; import java.util.Scan ...
- AJAX 加载效果(遮盖层)
//创建遮罩层函数体 function createMask(){ var node=document.createElement('div'); node.setAttribute('id','ba ...
- 洛谷P3285 [SCOI2014]方伯伯的OJ 动态开点平衡树
洛谷P3285 [SCOI2014]方伯伯的OJ 动态开点平衡树 题目描述 方伯伯正在做他的 \(Oj\) .现在他在处理 \(Oj\) 上的用户排名问题. \(Oj\) 上注册了 \(n\) 个用户 ...
- dom_bom学习
1. bom是什么? browser object model(浏览器对象模型) 在浏览器中 window指的就是bom对象 //网页重定向 // window.location.href=" ...
- FreeBSD 宣布 2020 年第 4 季度状态报告
FreeBSD 宣布 2020 年第 4 季度状态报告● 继续努力从 FreeBSD 基本系统中移除 GPL 协议的软件,以实现 FreeBSD 项目基本目标.● Linux 二进制兼容层的 Linu ...
- CSS浮动布局带来的高度塌陷以及其解决办法
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...
- Linux Python2 升级到 Python3
国内企业服务器用 CentOS 系统的比较多,CentOS8 系统已经将系统默认 Python 版本调整为了 Python3,但是 CentOS7 的存量还是很大,毕竟对企业生产服务来说稳定大于一切, ...