题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2112

HDU Today

Description

经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。

Input

输入数据有多组,每组的第一行是公交车的总数$N\ (0 \leq N \leq 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

最短路。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<map>
using std::map;
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::pair;
using std::vector;
using std::string;
using std::multimap;
using std::priority_queue;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef unsigned long long ull;
struct P {
int w, v;
P(int i = , int j = ) :w(i), v(j) {}
inline bool operator<(const P &a) const {
return w > a.w;
}
};
struct Node { int to, w, next; };
struct Dijkstra {
Node G[N];
map<string, int> A;
int start, end, tot, dist[], head[N];
inline void init() {
tot = , A.clear();
cls(head, -), cls(dist, 0x3f);
}
inline void add_edge(int u, int v, int w) {
G[tot] = { v, w, head[u] }; head[u] = tot++;
}
inline void built(int n) {
char str[][];
int u, v, w, k = ;
scanf("%s %s", str[], str[]);
A[str[]] = k++, A[str[]] = k++;
rep(i, n) {
scanf("%s %s %d", str[], str[], &w);
if (A.find(str[]) == A.end()) A[str[]] = k++;
if (A.find(str[]) == A.end()) A[str[]] = k++;
u = A[str[]], v = A[str[]];
add_edge(u, v, w), add_edge(v, u, w);
}
start = A[str[]], end = A[str[]];
}
inline void dijkstra() {
dist[start] = ;
priority_queue<P> q;
q.push(P(, start));
while (!q.empty()) {
P t = q.top(); q.pop();
int u = t.v;
if (dist[u] < t.w) continue;
for (int i = head[u]; ~i; i = G[i].next) {
int &w = dist[G[i].to];
if (w > dist[u] + G[i].w) {
w = dist[u] + G[i].w;
q.push(P(w, G[i].to));
}
}
}
printf("%d\n", dist[end] == (int)0x3f3f3f3f ? - : dist[end]);
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while (~scanf("%d", &n) && n != -) {
go.init();
go.built(n);
go.dijkstra();
}
return ;
}

最新文章

  1. 『.NET Core CLI工具文档』(九)dotnet-run
  2. HotSpot JVM常用参数设置
  3. Xenocode Postbuild 2010 for .NET 使用说明
  4. Spring Boot 添加Shiro支持
  5. 1.jenkins持续集成-jenkins安装
  6. 深度技术32位Win7系统Ghost版
  7. iOS 懒加载不起作用的原因
  8. 浅析LRU(K-V)缓存
  9. java中使用session的一些细节
  10. OpenSuSE查看指定软件包是否安装(OpenSuSE使用RPM作为默认的软件包维护管理工具)
  11. [置顶] MongoDB 分布式操作——分片操作
  12. 【iOS】网页中调用JS与JS注入
  13. Excel 删除所有错误公式
  14. java中部分知识点的验证实现
  15. SQLServer复习文档1(with C#)
  16. dnion的remap.conf文件
  17. Socket网络通信之数据传递
  18. App测试全(转自鲁德)
  19. cocos creator 无法打开项目 dock栏只显示图标问题解决方法
  20. Linux只读账号配置【转】

热门文章

  1. ORACLE 导空表结构
  2. python拷贝文件到多个文件夹
  3. 循环链表Josephus问题(c,cpp)
  4. note name
  5. 使用CORS:跨域两三事
  6. css选择器分类
  7. Windows phone 8 学习笔记(7) 设备(转)
  8. leetcode 101
  9. SVN创建Tags
  10. Android应用源码安卓短信拦截木马项目源码