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 ...
随机推荐
- Vue.js 实现的 3D Tab菜单
今天给大家带来一款基于VueJS的3D Tab菜单,它跟我们之前分享的许多CSS3 Tab菜单不同的是,它可以随着鼠标移动呈现出3D立体的视觉效果,每个tab页面还可以通过CSS自定义封面照片.它的核 ...
- MySQL 40题练习题和答案
2.查询"生物"课程比"物理"课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号, ...
- Linux下基础命令
(1)ls(查看列表) (2)ls -l(查看列出文件详细信息) (3)ls -al (查看全部列出文件详细信息) (4)ls -dl(查看目录信息) (5)pwd(查看当前工作的目录) ...
- 001-HashMap源码分析
HashMap源码分析 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如 memcached)的核心其实就是在内存中维护一张大的哈希表. 一.什 ...
- react+ts封装AntdUI的日期选择框之月份选择器DatePicker.month
需求:由于在项目开发中,当需要使用该组件时都需要对该组件进行大量的代码输出,为了方便代码统一管理,减少冗余代码,所以将此组件进行二次封装. 其他成员在使用中只需将自己的设置通过对应的参数传递到该组件, ...
- Cloudam云端,探索高性能计算在药物研究领域的解决方案
近日,Cloudam云端与国内某知名药企与合作,通过接入Cloudam云端自主研发的云E云超算服务,计算效率提高的数百倍.这也是云算力在生命科学领域的又一次成功应用.Cloudam云端云E云超算服务是 ...
- 【H264】视频编码发展简史
一.常见视频编码格式 编码格式有很多,如下图: 目前比较常用的编码有: H26x系列:由ITU(国际电传视讯联盟)主导,侧重网络传输 MPEG系列:由ISO(国际标准组织机构)下属的MPEG(运动图象 ...
- golang float32/64转string
v := 3.1415926535 s1 := strconv.FormatFloat(v, 'E', -1, 32)//float32s2 := strconv.FormatFloat(v, 'E' ...
- Java中BO、DAO、DO、DTO、PO、POJO、VO的概念
在程序开发中,经常会碰到各种专业术语,这里统一做一下解释,有遗漏或理解错误的恳请指正. BO(Business Object)业务对象 主要作用是把业务逻辑封装为一个对象,这个对象可以包括一个或多个其 ...
- 如何获取占用U盘的进程
依次打开开始---所有程序---附件---系统工具---资源监视器. 打开CPU标签栏,在"关联的句柄"中的搜索框中输入U盘的盘符,如G: 按回车搜索即可出结果. 在搜索结果中右键 ...