zoukankan      html  css  js  c++  java
  • HDU 2112 HDU Today

    HDU Today

    Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 35038    Accepted Submission(s): 8519


    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 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
     
    Author
    lgx
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1874 2066 1217 2680 1142 
    思路:spfa的板子,重点是注意利用map把字符串转成数字,当然hash也可以。
    错因:没有看到无解的情况。
    #include<map>
    #include<queue>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define MAXN 10010 
    using namespace std;
    char c[40];
    queue<int>que;
    map<string,int>ma;
    int n,tot,num,star,en;
    int dis[MAXN],vis[MAXN];
    int to[MAXN*2],net[MAXN*2],cap[MAXN*2],head[MAXN];
    void add(int u,int v,int w){
        to[++tot]=v;cap[tot]=w;net[tot]=head[u];head[u]=tot;
        to[++tot]=u;cap[tot]=w;net[tot]=head[v];head[v]=tot;
    }
    void spfa(int s){
        memset(vis,0,sizeof(vis));
        memset(dis,0x7f,sizeof(dis));
        while(!que.empty())    que.pop();
        que.push(s);vis[s]=1;dis[s]=0;
        while(!que.empty()){
            int now=que.front();
            que.pop();vis[now]=0;
            for(int i=head[now];i;i=net[i])
                if(dis[to[i]]>dis[now]+cap[i]){
                    dis[to[i]]=dis[now]+cap[i];
                    if(!vis[to[i]]){
                        vis[to[i]]=1;
                        que.push(to[i]);
                    }
                }
        }
    }
    int main(){
        while(scanf("%d",&n)&&n!=-1){
            cin>>c;if(!ma[c]) ma[c]=++num;star=ma[c];
            cin>>c;if(!ma[c]) ma[c]=++num;en=ma[c];
            for(int i=1;i<=n;i++){
                int u,v,w;
                cin>>c;if(!ma[c])    ma[c]=++num;u=ma[c]; 
                cin>>c;if(!ma[c])    ma[c]=++num;v=ma[c];
                cin>>w;add(u,v,w);
            }
            spfa(star);
            if(dis[en]!=2139062143)    cout<<dis[en]<<endl;
            else cout<<"-1"<<endl;
            tot=0;num=0;ma.clear();
            memset(head,0,sizeof(head));
        }
    }
    /*
    6
    xiasha westlake
    xiasha station 60
    xiasha ShoppingCenterofHangZhou 30
    station westlake 20
    ShoppingCenterofHangZhou supermarket 10
    xiasha supermarket 50
    supermarket westlake 10
    -1
    */
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    System 类的使用
    StringBuffer 与 StringBuilder类的使用
    String 类 的 使用
    多线程
    音乐播放
    数据库
    表示图编辑
    UITextView(2)
    UITextView
    tarBar
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/8968596.html
Copyright © 2011-2022 走看看