HDU Today

来源:互联网 发布:抱枕材料 知乎 编辑:程序博客网 时间:2024/06/06 05:47

这道题目并不难,只是自己很天真的认为两个站点之间是单向的,还有就是当起点和终点一致时直接输出0就行了

对于那些冗长的地名可以直接用map构造一个string到int的映射

经过锦囊相助,海东集团终于度过了危机,从此,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
6xiasha westlakexiasha station 60xiasha ShoppingCenterofHangZhou 30station westlake 20ShoppingCenterofHangZhou supermarket 10xiasha supermarket 50supermarket westlake 10-1
Sample Output
50Hint:The best route is:xiasha->ShoppingCenterofHangZhou->supermarket->westlake虽然偶尔会迷路,但是因为有了你的帮助**和**从此还是过上了幸福的生活。――全剧终――

#include<stdio.h>#include<string.h>#include<iostream>#include<vector>#include<queue>#include<map>#include<string>#include<algorithm>#define maxn 10005#define inf 0x3f3f3f3fusing namespace std;void dijkstra(int s);struct list{int des;int weight;list() {}list(int a,int b){des=a;weight=b;}friend bool operator <(struct list c,struct list d){if(c.weight==d.weight)return c.des<d.des;elsereturn c.weight>d.weight;}};vector<struct list> an[maxn];map<string,int> mp;int dis[maxn],n;bool book[maxn];int main(){while(scanf("%d",&n),n>=0){for(int i=0;i<=n;i++)an[i].clear();mp.clear();int cnt=2;string str1,str2;cin >> str1 >> str2;mp[str1]=1;mp[str2]=n;for(int i=0;i<n;i++){int a,b,cost;string start,end;cin >> start >> end >> cost;if(mp[start]==0){mp[start]=cnt;a=cnt;cnt++;}elsea=mp[start];if(mp[end]==0){mp[end]=cnt;b=cnt;cnt++;}elseb=mp[end];an[a].push_back(list(b,cost));an[b].push_back(list(a,cost));} if(str1==str2){printf("0\n");continue;}dijkstra(1);if(dis[n]==inf)printf("-1\n");elseprintf("%d\n",dis[n]);}return 0;}void dijkstra(int s){fill(dis,dis+n+1,inf);memset(book,false,sizeof(book));priority_queue<struct list> q;struct list reco;reco.des=1;reco.weight=0;dis[1]=0;q.push(reco);while(!q.empty()){struct list Q=q.top();if(book[Q.des]==true){q.pop();continue;}book[Q.des]=true;q.pop();for(int i=0;i<an[Q.des].size();i++){reco=an[Q.des][i];if(dis[reco.des]>Q.weight+reco.weight){dis[reco.des]=Q.weight+reco.weight;q.push(list(reco.des,dis[reco.des]));}}}}


原创粉丝点击