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 ...
随机推荐
- Java开发不懂Docker,学尽Java也枉然,阿里P8架构师手把手带你玩转Docker实战
转: Java开发不懂Docker,学尽Java也枉然,阿里P8架构师手把手带你玩转Docker实战 Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一 ...
- Docker 一图胜千言
Docker 一图胜千言 0 为什么 Docker 比虚拟机快? 注:少了一层抽象层( Hypervisor ) 1 Docker Logo 要素: 大海 宿主机 Host 鲸鱼 Docker 集装箱 ...
- 2019HDU多校第七场 HDU6646 A + B = C 【模拟】
一.题目 A + B = C 二.分析 比较考验码力的题. 对于$c$,因为首位肯定不为0,那么$a$或者$b$至少有一个最高位是和$c$平齐的,或者少一位(相当于$a$+$b$进位得到). 那么这里 ...
- 最权威的html 标签属性大全
<p>---恢复内容开始---</p>1.html标签 <marquee>...</marquee>普通卷动 <marquee behavior= ...
- 11、pass,is,位运算的补充
pass的补充 一般Python的代码是基于:和缩进来实现,Python中规定代码块中必须要有代码才算完整,在没有代码的情况下为了保证语法的完整性可以用pass代替 if 条件: pass else: ...
- 10、字典dict
字典(dict) 字典是有序,键不重复,元素只能是键值对的可变的容器 dict = {'k1':'v1','k2':'v2'} 容器 元素必须键值对 键不重复,重复则会被覆盖 data = {'k1' ...
- MySQL基础知识:MySQL Connection和Session
在connection的生命里,会一直有一个user thread(以及user thread对应的THD)陪伴它. Connection和Session概念 来自Stackoverflow的一个回答 ...
- 找单词 HDU - 2082(普通母函数)
题目链接:https://vjudge.net/problem/HDU-2082 题意:中文题. 思路:构造普通母函数求解. 母函数: 1 #include<time.h> 2 #incl ...
- PTA 有序数组的插入
6-5 有序数组的插入 (20 分) 本题要求将任一给定元素插入从大到小排好序的数组中合适的位置,以保持结果依然有序. 函数接口定义: bool Insert( List L, ElementTy ...
- Makefile基本用法
来源 https://www.gnu.org/software/make/manual/make.pdf 简单的例子 其中的cc通过链接,间接指向/usr/bin/gcc. Makefile文件中列出 ...