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},然后取集合里 ...
随机推荐
- Orcad CIS怎么批量修改字体大小
选中DSN,右键,design properties, schematic design,选择design properties.
- Nginx https免费SSL证书配置指南
生成证书 $ cd /usr/local/nginx/conf $ openssl genrsa -des3 -out server.key 1024 $ openssl req -new -key ...
- Cookie的写入,和读取
public static void SetLoginGmameInfo(string uid, string sid, string timestring, string sign) ...
- python学习(三)数字类型示例
奶奶的报了这个错,我以为可以像java中字符串加数字的嘛 Traceback (most recent call last): File "./number.py", line ...
- 计算两个GPS坐标点的距离
计算两个GPS坐标点的距离,第一个参数是第一个点的维度,第二个参数是第一个点的经度 http://yuninglovekefan.blog.sohu.com/235655696.html /** * ...
- 微信小程序设置控件权重
项目中最常用的两种布局方式,水平布局和垂直布局,在微信小程序中实现起来也比较简单. 1.横向水平布局: 实现水平布局,需要四个view容器组件,其中一个是父容器.如下: & ...
- Python中的注解“@” 、Java 注解
https://blog.csdn.net/u013474436/article/details/75675113 https://blog.csdn.net/briblue/article/deta ...
- POJ2594 Treasure Exploration[DAG的最小可相交路径覆盖]
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8301 Accepted: 3 ...
- 九度OJ 1016:火星A+B (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4913 解决:1334 题目描述: 读入两个不超过25位的火星正整数A和B,计算A+B.需要注意的是:在火星上,整数不是单一进制的, ...
- javascript实现日期时间动态显示
.aspx代码例如以下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Def ...