ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)
Description
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
Input
第二行有徐总的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
note:一组数据中地名数不会超过150个。
如果N==-1,表示输入结束。
Output
Sample Input
Sample Output
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake
**和**从此还是过上了幸福的生活。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; typedef pair<int, int> pii;
const int maxN = ; //链式前向星
struct Edge
{
int to, next;
int val;
}edge[maxN*]; int head[maxN], cnt; void addEdge(int u, int v, int val)
{
edge[cnt].to = v;
edge[cnt].val = val;
edge[cnt].next = head[u];
head[u] = cnt;
cnt++;
} void initEdge()
{
memset(head, -, sizeof(head));
cnt = ;
} int n;
map<string, int> Hash;
string from, to;
int fromId, toId;
int dis[maxN]; void input()
{
initEdge();
Hash.clear();
memset(dis, -, sizeof(dis));
int num = ;
cin >> from >> to;
Hash[from] = num++;
if (!Hash[to])
Hash[to] = num++;
string u, v;
int cost;
for (int i = ; i < n; ++i)
{
cin >> u >> v >> cost;
if (!Hash[u])
Hash[u] = num++;
if (!Hash[v])
Hash[v] = num++;
addEdge(Hash[u], Hash[v], cost);
addEdge(Hash[v], Hash[u], cost);
}
fromId = Hash[from];
toId = Hash[to];
dis[fromId] = ;
} void SPFA()
{
pii k;
priority_queue <pii, vector<pii>, greater<pii> > q;
q.push(pii(dis[fromId], fromId));
int x;
while (!q.empty())
{
k = q.top();
q.pop();
x = k.second;
for (int i = head[x]; i != -; i = edge[i].next)
{
if (dis[edge[i].to] == - || dis[edge[i].to] > dis[x]+edge[i].val)
{
dis[edge[i].to] = dis[x]+edge[i].val;
q.push(pii(dis[edge[i].to], edge[i].to));
}
}
}
} int main()
{
//freopen("test.in", "r", stdin);
while (scanf("%d", &n) != EOF && n != -)
{
input();
SPFA();
printf("%d\n", dis[toId]);
}
return ;
}
ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)的更多相关文章
- ACM学习历程—Rotate(HDU 2014 Anshan网赛)(几何)
Problem Description Noting is more interesting than rotation! Your little sister likes to rotate thi ...
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- hdu 2112 HDU Today
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的 ...
- HDU 2112 HDU Today(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others ...
- ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)
Description We all use cell phone today. And we must be familiar with the intelligent English input ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- ACM学习历程—HDU 3949 XOR(xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
随机推荐
- 深度解析 | 秒懂AI+智慧手机实践
阅读数:17 随着人工智能的概念越来越深入人心,智慧化生活和对应的智慧化终端体验也吸引越来越多的目光.可以想见,人工智能会深刻改变终端产业,但目前也面临各种挑战和问题.此前,在南京软件大会上,华 ...
- 【Python】IDLE启动错误
启动IDLE时报Subprocess Startup Error错误 错误信息 IDLE's subprocess didn't make connection.Either IDLE cant't ...
- 认识 service worker
离线缓存可以提升用户体验,可以节省网络资源,但是,浏览器对资源缓存和自定义网络请求的控制一直不够完善,service worker 的出现就是为了解决这些问题 它可以解决目前离线应用的问题,同时也可以 ...
- 06 redis中set结构及命令详解
集合 set 相关命令 集合的性质: 唯一性,无序性,确定性 注: 在string和link的命令中,可以通过range 来访问string中的某几个字符或某几个元素 但,因为集合的无序性,无法通过下 ...
- swift基础教程笔记
http://www.imooc.com/learn/127 <玩儿转swift> 慕课网教程笔记,自己根据2.1的语法做了更新. I. 1.通过playground来学习.熟悉swift ...
- jQuery+bootstrap实现美化警告/确认/提示对话框插件
http://www.html580.com/12067/demo http://craftpip.github.io/jquery-confirm/
- Struts2 ModelDriven接口使用
用户在做http请求时一般都有两种方式:get和post方式.get方式用来获取查询相关信息,既向服务器获得信息,而post方式用来更新信息既向服务器提交数据.通常情况下,用get方式向服务器获取信息 ...
- Mybatis资料
1. 入门案例 https://www.cnblogs.com/xdp-gacl/p/4261895.html 2. 详细笔记 以及配套视频教程: 笔记:https://blog.csdn.net/S ...
- python classmethod方法 和 staticmethod
classmethod() 是一个类方法,用来装饰对应的函数.被classmethod 装饰之后就无需实例化,也不需要在函数中传self,但是被装饰的函数第一个参数需要是cls来表示自身类.可以用来调 ...
- 谁能举个通俗易懂的例子告诉我IAAS,SAAS,PAAS的区别?【转自知乎】
是时候祭出这篇吃货文章了: ———————————————————— ———————————————————— ———————————————————— &amp;amp;amp;lt ...