HDU Today

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23854    Accepted Submission(s): 5743

Problem Description
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
 
Input
输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);
第二行有徐总的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
note:一组数据中地名数不会超过150个。
如果N==-1,表示输入结束。
 
Output
如果徐总能到达目的地,输出最短的时间;否则,输出“-1”。
 
Sample Input
6
xiasha westlake
xiasha station 60
xiasha ShoppingCenterofHangZhou 30
station westlake 20
ShoppingCenterofHangZhou supermarket 10
xiasha supermarket 50
supermarket westlake 10
-1
 
Sample Output
50

Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake

虽然偶尔会迷路,但是因为有了你的帮助
**和**从此还是过上了幸福的生活。

――全剧终――

 

这道题让我知道了hash选取函数的重要性,用普通SPFApop之后要让vis[now]=0(强行WA11次)。

对于这样的更贴近生活的地名最短路问题,有两种做法,一种是用hash把字符串hash成一个特定的值,然后spfa,另一种是给字符串标号,用map是因为只标第一次出现的名字。hash是我自己想的,WA0x3ffffffff次,最后不知道是种子运气选的刚好还是把vis[now]加上的缘故,出现了久违的AC;后者是hashWA半天看别人博客看来的,效率前者780+ms后者1700+ms,前者麻烦后者简单。具体自己看吧。

hash方法代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
using namespace std;
typedef long long LL;
const int N=20100;
struct info
{
int to;
int dx;
int pre;
}E[N];
int head[N],d[N],cnt;
char s[35],t[35],x[35],y[35];
inline void add(const int &s,const int &t,const int &dis)
{
E[cnt].to=t;
E[cnt].dx=dis;
E[cnt].pre=head[s];
head[s]=cnt++;
}
inline int hash_fun(char s[])
{
int r=0,len=strlen(s);
for (int i=0; i<len; i++)
{
r=r*17+s[i];
r%=20023;
}
return r%20023;
}
inline void init()
{
memset(head,-1,sizeof(head));
MMINF(d);
cnt=0;
}
inline void spfa(int s)
{
typedef pair<int,int> pii;
priority_queue<pii>Q;
d[s]=0;
Q.push(pii(-d[s],s));
while (!Q.empty())
{
int now=Q.top().second;
Q.pop();
for (int i=head[now]; i!=-1; i=E[i].pre)
{
int v=E[i].to;
int w=E[i].dx;
if(d[v]>d[now]+w)
{
d[v]=d[now]+w;
Q.push(pii(-d[v],v));
}
}
}
}
int main(void)
{
int n,i,z;
while (~scanf("%d",&n)&&n!=-1)
{
init();
scanf("%s%s",s,t);
int sta=hash_fun(s),des=hash_fun(t);
for (i=0; i<n; i++)
{
scanf("%s %s %d",x,y,&z);
int S=hash_fun(x),T=hash_fun(y);
add(S,T,z);
add(T,S,z);
}
spfa(sta);
d[des]==INF?puts("-1"):printf("%d\n",d[des]);
}
return 0;
}

map标号法:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
using namespace std;
typedef long long LL;
const int N=20100;
struct info
{
int to;
int dx;
int pre;
}E[N];
int head[N],d[N],cnt;
char s[50],t[50],x[50],y[50];
void add(int s,int t,int dis)
{
E[cnt].to=t;
E[cnt].dx=dis;
E[cnt].pre=head[s];
head[s]=cnt++;
}
void init()
{
memset(head,-1,sizeof(head));
memset(d,INF,sizeof(d));
cnt=0;
MM(E);
}
void spfa(int s)
{
typedef pair<int,int> pii;
priority_queue<pair<int,int> >Q;
d[s]=0;
Q.push(pii(-d[s],s));
while (!Q.empty())
{
int now=Q.top().second;
Q.pop();
for (int i=head[now]; i!=-1; i=E[i].pre)
{
int v=E[i].to;
int w=E[i].dx;
if(d[v]>d[now]+w)
{
d[v]=d[now]+w;
Q.push(pii(-d[v],v));
}
}
}
}
int main(void)
{
int n,i,j,z,cnt_e;
while (~scanf("%d",&n)&&n!=-1)
{
init();
cnt_e=0;
map<string,int>pos;
scanf("%s%s",s,t);
pos[s]=++cnt_e;
if(pos[t]==0)
pos[t]=++cnt_e;
for (i=0; i<n; i++)
{
scanf("%s%s%d",x,y,&z);
if(pos[x]==0)
pos[x]=++cnt_e;
if(pos[y]==0)
pos[y]=++cnt_e; add(pos[x],pos[y],z);
add(pos[y],pos[x],z);
}
spfa(pos[s]);
if(d[pos[t]]==INF)
puts("-1");
else
printf("%d\n",d[pos[t]]);
}
return 0;
}

HDU——2112HDU Today(SPFA+简单Hash或map+前向星)的更多相关文章

  1. HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】

    最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...

  2. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  3. Hash与Map

    Hash与Map 面试时经常被问到,什么是Hash?什么是Map? 答:hash采用hash表存储,map一般采用红黑树(RB Tree)实现.因此其memory数据结构是不一样的,而且他们的时间复杂 ...

  4. hdu What Are You Talking About(map)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 map简单应用 代码: #include <stdio.h> #include &l ...

  5. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

  6. HDOJ-ACM1425 sort 简单hash应用

    其实快排也可以通过这个问题~不是考点 没想到考点是这个,简单hash应用,空间换时间 初始化一个长度为1000001的数组(由于数字的范围为[-500000,500000]) 如果存在这个数m,数组下 ...

  7. HDU 1535 SPFA 前向星存图优化

    Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  8. HDU - 1535 Invitation Cards 前向星SPFA

    Invitation Cards In the age of television, not many people attend theater performances. Antique Come ...

  9. POJ——1364King(差分约束SPFA判负环+前向星)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description ...

随机推荐

  1. MD5 介绍

    MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权费用. MD5功能: 输入任意 ...

  2. strophe.js 插件 XMPP openfire

    参考资料:http://strophe.im/strophejs/ https://github.com/strophe/strophejs-plugins http://amazeui.org/ 最 ...

  3. Spring 配置定时器(注解+xml)方式—整理

    一.注解方式 1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="http://www.springframework.or ...

  4. 使用Cordova将您的前端JavaScript应用打包成手机原生应用

    假设我用JavaScript和HTML开发了一个前端应用,我想把该应用打包成能直接在手机上安装和运行(不通过浏览器)的原生应用,例如像下面这样.对应用的用户来说,他们得到的用户体验和真正的用Andro ...

  5. rhythmbox插件开发笔记2:背景知识学习 D-Bus&VFS&Gio& Python GTK+ 3

    这次主要简单介绍下相关的背景知识 D-Bus&VFS&Gio& Python GTK+ 3  D-Bus D-Bus是开源的进程通信(IPC)系统,它允许多个进程进行实时通信. ...

  6. IOS7.1 企业应用 证书无效 已解决

    http://www.cocoachina.com/bbs/read.php?tid=194213&keyword=7.1 关于IOS7.1企业版发布后,用户通过SARAFI浏览器安装无效的解 ...

  7. js 下载文件/导出

    const url = '/sasd/fsd/xxxx/exportMailData2Excel'this.downloadFile(url, 'blob', this.isSearch) // 调用 ...

  8. 基于Python的Web应用开发实战——2 程序的基本结构

    2.1 初始化 所有Flaks程序都必须创建一个程序实例. Web服务器使用一种名为Web服务器网关接口(Web Server Gateway Interface,WSGI)的协议,把接收自客户端的所 ...

  9. 快学UiAutomator各种框架介绍

    Monkey 编写语言:命令行 运行环境:使用adb连接PC运行测试对象:Android平台自动化测试的一种手段,通过Monkey程序模拟用户触摸屏幕.滑动Trackball.按键等操作来对设备上的程 ...

  10. Use-After-Free

    0x00 UAF利用原理 uaf漏洞产生的主要原因是释放了一个堆块后,并没有将该指针置为NULL,这样导致该指针处于悬空的状态(这个指针可以称为恶性迷途指针),同样被释放的内存如果被恶意构造数据,就有 ...