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 ...
随机推荐
- 详解JavaScript中的原型
前言 原型.原型链应该是被大多数前端er说烂的词,但是应该还有很多人不能完整的解释这两个内容,当然也包括我自己. 最早一篇原型链文章写于2019年07月,那个时候也是费了老大劲才理解到了七八成,到现在 ...
- 《Selenium自动化测试实战》新书上市,有需要朋友们可以了解下,欢迎大家多提宝贵意见
京东:https://item.jd.com/13123910.html当当:http://product.dangdang.com/29204520.html 1. 本书基于 Python 3.8 ...
- pytorch(05)计算图
张量的一系列操作,增多,导致可能出现多个操作之间的串行并行,协同不同的底层之间的协作,避免操作的冗余.计算图就是为了解决这些问题产生的. 计算图与动态图机制 1. 计算图 计算图用来描述运算的有向无环 ...
- golang-Zap和Go Logger日志库
目录 在Go语言项目中使用Zap日志库 介绍 默认的Go Logger日志库 实现Go Logger 设置Logger 使用Logger Logger的运行 Go Logger的优势和劣势 优势 劣势 ...
- 在onBackPress中实现退出拦截时不生效
现象描述 在快应用中弹出一个弹窗,期望效果是该弹窗在用户确认后再退出,但是使用onbackpress控制确认弹窗后自动退出不生效. 问题分析 快应用引擎实现机制决定了onbackpress不能有耗时的 ...
- scrapy框架爬取图片并将图片保存到本地
如果基于scrapy进行图片数据的爬取 在爬虫文件中只需要解析提取出图片地址,然后将地址提交给管道 配置文件中:IMAGES_STORE = './imgsLib' 在管道文件中进行管道类的制定: f ...
- PHP配置 2. 日志相关配置
例如,在disable_functions,定义禁用phpinfo函数, # vim /usr/local/php/etc/php.ini disable_functions=phpinfo,eval ...
- 在B站刷视频多倍速操作
B站多倍数播放 1. 最初天真版 F12 或者笔记本(Fn+F12) console控制台 输入 document.querySelector('video').playbackRate = 4: - ...
- 如何使用Docker部署Go Web应用
目录 如何使用Docker部署Go Web应用 Docker部署示例 准备代码 创建Docker镜像 编写Dockerfile Dockerfile解析 From Env WORKDIR,COPY,R ...
- Android学习之CoordinatorLayout+FloatingActionButton+Snackbar
CoordinatorLayout •简介 CoordinatorLayout 协调布局,可以理解为功能更强大的 FrameLayout 布局: 它在普通情况下作用和 FrameLayout 基本一致 ...