hdu 2112 HDU Today
题目连接
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 ;
- }
hdu 2112 HDU Today的更多相关文章
- HDU 2112 HDU Today(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others ...
- hdu 2112 HDU Today (floyd算法)
这道题貌似在原来学长给我们的搞的小比赛中出过! 这次又让我遇到,果断拿下! 不过方法很蠢,跑了1000多ms,虽然要求5000ms以内! 题目就是给你一些位置之间的距离,然后再让你求特定的两点之间的距 ...
- hdu 2112 HDU Today (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的 ...
- hdu 2112 HDU Today 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目意思:又是求最短路的,不过结合埋字符串来考查. 受之前1004 Let the Balloo ...
- HDU 2112 HDU Today (Dijkstra算法)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 2112 HDU Today(最短路径+map)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 2112 HDU Today 最短路
题目描述: Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这 ...
- hdu 2112 HDU Today(map与dijkstra的结合使用)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 2112 HDU Today(STL MAP + Djistra)
题目链接:HDU Today 立即集训要開始,抓紧时间练练手,最短路的基础题,第一次用STL的map 题目非常水,可是错了N遍.手贱了.本题不优点理的就是把地名转化为数字 #include <i ...
随机推荐
- 慕课网-安卓工程师初养成-4-6 Java条件语句之 switch
来源:http://www.imooc.com/code/1358 当需要对选项进行等值判断时,使用 switch 语句更加简洁明了.例如:根据考试的名次,给予前 4 名不同的奖品.第一名,奖励笔记本 ...
- com学习(五)——实现多接口
从第五回开始到第七回,咱们用 ATL 写了一个简单的 COM 组件,之所以说简单,是因为在组件中,只实现了一个自定义(custom)的接口 IFun.当然如果想偷懒的话,我们可以把 200 个函数都加 ...
- php array(object) 与xml相互转换
private function _array_to_xml($source, $charset='utf-8'){ $array = json_decode($source); $pre = '&l ...
- 在集群环境中使用 EhCache 缓存系统|RMI 集群模式
RMI 是 Java 的一种远程方法调用技术,是一种点对点的基于 Java 对象的通讯方式.EhCache 从 1.2 版本开始就支持 RMI 方式的缓存集群.在集群环境中 EhCache 所有缓存对 ...
- Ossim应用体验视频
Ossim体验视频 近期,我写的有关Ossim应用的系列文章网友们非常关注,这里对大家提出有一些问题我制作了高清的视频和截图发布到网站,以让更多的人了解这款开源安全平台.在年后出版的教程中会详细讲解o ...
- ng-class ionic
我发现 ng-class="{yourclass:true,outerclass:false}" 竟然不起作用...囧.... 幸好有Google .... <p ng-c ...
- C#多线程案例基础
C#多线程案例基础(转) 在学习多线程之前,我们先来看几个概念: 1,什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源,当然一个程序也可能开 ...
- JPA 批量新增
1. 在实现类 增加 EntityManager 注入 private EntityManager em; @PersistenceContext(name = "EntityManager ...
- C#中string类型前加@标志的作用
转自:http://stackoverflow.com/questions/4879152/c-sharp-before-a-string (stackoverflow) string字符串前加@ ...
- hdu2571
if(x==1) f(x,y)=max{f(x,y-1),f(x,z)} {y%z==0&&1<y/z<=y&&1<=z<<y} els ...