HDU Today2112

来源:互联网 发布:下载个淘宝买东西的 编辑:程序博客网 时间:2024/06/07 02:43
HDU Today
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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

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虽然偶尔会迷路,但是因为有了你的帮助**和**从此还是过上了幸福的生活。
spfa。
map容器建图,cnt记录站点之间路线条数,
<pre name="code" class="cpp">#include <cstdio>#include <iostream>#include <cstring>#include <queue>#include <map>#define N 155#define MAX 0x7ffffffusing namespace std;int n,cnt;int vist[N],d[N],mapp[N][N];int spfa(int s,int t){    if(s==t)    return 0;    for(int i=1;i<=cnt;i++)        d[i]=MAX,vist[i]=0;    queue<int> q;    d[s]=0;    vist[s]=1;    q.push(s);    while(!q.empty())    {        int now=q.front();        q.pop();        vist[now]=0;        for(int i=1;i<=cnt;i++)        {            if(d[i]>d[now]+mapp[now][i])            {                d[i]=d[now]+mapp[now][i];                if(!vist[i])   vist[i]=1,q.push(i);            }        }    }    if(d[t]>=MAX)   return -1;    return d[t];}int main(){    while(~scanf("%d",&n)&&n!=-1)    {        for(int i=1;i<N;i++)            for(int j=1;j<N;j++)                mapp[i][j]=MAX;        map<string,int>mp;        mp.clear();        char s[110],t[110];        scanf("%s%s",&s,&t);        cnt=0;        mp[s]=++cnt;        if(mp[t]==0)    mp[t]=++cnt;        for(int i=0;i<n;i++)        {            int c;            char ss[110],tt[110];            scanf("%s%s%d",&ss,&tt,&c);            if(mp[ss]==0)    mp[ss]=++cnt;            if(mp[tt]==0)    mp[tt]=++cnt;            if(mapp[mp[ss]][mp[tt]]>c)                mapp[mp[ss]][mp[tt]]=mapp[mp[tt]][mp[ss]]=c;        }        cout<<spfa(mp[s],mp[t])<<endl;    }}




0 0