HDU 2112 HDU Today(Dijkstra算法)
2018-01-23 23:35
363 查看
HDU Today
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 33170 Accepted Submission(s): 8061
Problem Description
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
Input
输入数据有多组,每组的第一行是公交车的总数N(0<=N<=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
Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake
虽然偶尔会迷路,但是因为有了你的帮助
**和**从此还是过上了幸福的生活。
――全剧终――补充一点生活常识,公交车是在两地之间来回跑的。开始以为是单向边,wa了好多次还有这题目特坑的地方就是按照数据范围来说不算大,可以用弗洛伊德算法,但是交了一发超时,五秒钟都不够,最后用Dijstra过的
#include<iostream> #include<cstdio> #include<string.h> #include<string> #include<algorithm> #include<string> #include<map> #include<vector> #include<queue> #include<bits/stdc++.h> const int INF=0x3f3f3f3f; int n,d[155],y; using namespace std; map<string,int>mp; struct node { int to,cost; }; vector<node>rode[155]; struct node2 { int dec,len; }; bool operator<(node2 a,node2 b) { return a.len>b.len; } priority_queue<node2>P; void init() { for(int i=1;i<=150;i++) d[i]=INF; d[1]=0; } void Dij() { node2 r;r.dec=1;r.len=0;P.push(r); while(!P.empty()) { node2 t=P.top();P.pop(); if(d[t.dec]<t.len)continue; int L=rode[t.dec].size(); for(int i=0;i<L;i++) { node e=rode[t.dec][i]; if(d[e.to]>d[t.dec]+e.cost) { d[e.to]=d[t.dec]+e.cost; node2 z;z.dec=e.to;z.len=d[e.to]; P.push(z); } } } } int main() { int i,j,k; while(scanf("%d",&n)!=EOF) { if(n==-1)break; init(); string s,t;s.resize(40);t.resize(40); scanf("%s %s",&s[0],&t[0]); if(s==t)d[2]=0; mp[s]=1;mp[t]=2;y=3; for(i=1;i<=n;i++) { string r,q;int w;r.resize(40);q.resize(40); scanf("%s %s %d",&r[0],&q[0],&w); if(mp.find(r)==mp.end()) mp[r]=y++; if(mp.find(q)==mp.end()) mp[q]=y++; pair<string,int> it1=*mp.find(r); pair<string,int> it2=*mp.find(q); int v=it1.second,u=it2.second; node ww;ww.to=u;ww.cost=w; rode[v].push_back(ww);ww.to=v; rode[u].push_back(ww); } Dij(); if(d[2]<INF)printf("%d\n",d[2]); else printf("-1\n"); mp.clear(); for(i=0;i<=150;i++) rode[i].clear(); } return 0; }
相关文章推荐
- hdu 2112 HDU Today 最短路(Dijkstra算法)
- HDU 2112 HDU Today (Dijkstra算法)
- HDU 2112 HDU Today 字典树+最短路dijkstra算法
- HDU 2112 HDU Today(dijkstra算法)
- HDU 2112 HDU Today (Dijkstra算法)
- HDU-2112-HDU Today
- hdu 2112 HDU Today(最短路+字典树)
- hdu 2112 HDU Today
- hdu 2112 HDU Today(SPFA+map)
- hdu 2112 HDU Today
- hdu 2112 HDU Today(map与dijkstra的结合使用)
- hdu 2112 HDU Today ( Dijkstra )
- HDU 2112 HDU Today(STL MAP + Djistra)
- hdu 2112 HDU Today
- hdu 2112 HDU Today (Dijkstra + map)
- HDU 2112 HDU Today(STL MAP + Djistra)
- HDU 2112 HDU Today
- HDU-2112 HDU Today(最短路[Dijkstra])
- HDU 2112 HDU Today(最短路,dijkstra)
- HDU 2112— HDU Today,最短路径算法,Dijkstra