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},然后取集合里 ...
随机推荐
- 优志愿前端数据加密破解-python
# coding=utf-8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "etiaky.sett ...
- gcc编译静态库和动态库
今天要用到静态库和动态库,于是写了几个例子来巩固一下基础.hello1.c ———————————————————— #include <stdio.h>void print1(int ...
- 【转】cmd chcp命令切换字符格式
cmd chcp命令切换字符格式 命令介绍: chcp 65001 #换成utf-8代码页 chcp 936 #换成默认的gbk chcp 437 #美国英 ...
- 最新番茄花园win7系统快速稳定版
这是最新番茄花园win7系统64位快速稳定版 V2016年2月,该系统由系统妈整理和上传,系统具有更安全.更稳定.更人性化等特点.集成最常用的装机软件,集成最全面的硬件驱动,精心挑选的系统维护工具,加 ...
- Flask,ORM及模板引擎Jinja2
跨域:http://blog.csdn.net/yannanxiu/article/details/53036508 下载flask_cors包 pip install flask-cors 使用fl ...
- myeclipse中disable maven nature怎么恢复
eclipse更新maven的时候,不小心手一抖,点上了Disable Maven Nature,然后工程右键菜单中的Maven栏就不见了! 其实这是把maven工程转换成了一般工程,再转回来就好了. ...
- java array
1 array变量 Type[] array_virable_name; 2 array对象 2.1 new Type[] array_virable_name = new Type[NUM]; 2. ...
- redis自启动
$ vi /etc/init.d/redis # chkconfig: 2345 90 10 # description: Redis is a persistent key-value databa ...
- win10+UEFI下u盘安装ubuntu16.04
本人电脑是华硕,由于要求使用linux所以安装: 1.首先给linux划出一个大分区,感觉最少50G: win10下磁盘管理,在最后的分区中压缩出50g,空间,其他的不用问了,也不用继续分区,一个大的 ...
- Java for LeetCode 133 Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...