zoukankan      html  css  js  c++  java
  • hdu 2112 Today【F map + Floyd 入门训练】

    HDU Today

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


    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
     
    算法 :STL map + floyd 或(map + Dijkstra)
    思路:简单的最短路算法,关键是如何处理地点。用 map 创建地点对数字的一一映射。
    总结 :深入了解了 map 的用法
      例如:本题的 map<string, int> m
                        插入的 string 是关键字key, int 是关键字所对应的值value (m[key] = value)
            由于关键字的唯一性,如果重复插入,那么只认最新的。
                        如果当前地点的string已经插入到了map m中,则map[string] != 0 可以直接判断。

    方法1:map+floyd
    F Accepted 400 KB 2281 ms C++ 1271 B 2013-06-03 20:22:37
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<map>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    const int maxn = 160;
    const int INF = 1000000;
    
    int g[maxn][maxn];
    map<string, int> m;
    
    int main()
    {
        int n;
        while(scanf("%d", &n) != EOF)
        {
            if(n == -1) break;
    
            m.clear();
            int cnt = 0;
    
            for(int i = 0; i < maxn; i++)
            {
                for(int j = 0; j < maxn; j++)
                {
                    g[i][j] = (i == j ? 0 : INF);
                }
            }
    
            string start, end;
            cin>>start>>end;
            if(!m[start]) m[start] = cnt++;
            if(!m[end]) m[end] = cnt++;
    
            string s,e;
            int t;
            for(int i = 1; i <= n; i++)
            {
                cin>>s>>e>>t;
                if(!m[s]) m[s] = cnt++;
                if(!m[e]) m[e] = cnt++;
    
                if(g[m[s]][m[e]] > t) g[m[s]][m[e]] = g[m[e]][m[s]] = t;
            }
    
            for(int k = 0; k < cnt; k++)
            {
                for(int i = 0; i < cnt; i++)
                {
                    for(int j = 0; j < cnt; j++)
                    g[i][j] = min(g[i][j], g[i][k]+g[k][j]);
                }
            }
    
            if(g[m[start]][m[end]] == INF) printf("-1\n");
            else printf("%d\n", g[m[start]][m[end]]);
    
        }
        return 0;
    }

    方法2:map+Dijkstra
    F Accepted 400 KB 2031 ms C++ 1453 B 2013-06-03 20:38:30
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<map>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    const int maxn = 160;
    const int INF = 1000000;
    
    bool vis[maxn];
    int g[maxn][maxn];
    int d[maxn];
    map<string, int> m;
    
    int N,n;
    string start,end;
    
    void Dijkstra()
    {
        memset(vis, false, sizeof(vis));
        for(int i = 0; i < n; i++)
        {
            d[i] = g[m[start]][i];
        }
        vis[m[start]] = true;
        d[m[start]] = 0;
    
        for(int i = 0; i < n; i++)
        {
            int x, y, m = INF;
            for(y = 0; y < n; y++) if(!vis[y] && m >= d[y]) m = d[x=y];
            vis[x] = true;
            for(y = 0; y < n; y++) d[y] = min(d[y], d[x]+g[x][y]);
        }
    }
    int main()
    {
        while(scanf("%d", &N) != EOF)
        {
            if(N == -1) break;
    
            m.clear();
            int cnt = 0;
    
            for(int i = 0; i < maxn; i++)
            {
                for(int j = 0; j < maxn; j++)
                    g[i][j] = (i == j ? 0 : INF);
            }
    
            cin>>start>>end;
            if(!m[start]) m[start] = cnt++;
            if(!m[end]) m[end] = cnt++;
    
            string s,e;
            int t;
            for(int i = 1; i <= N; i++)
            {
                cin>>s>>e>>t;
                if(!m[s]) m[s] = cnt++;
                if(!m[e]) m[e] = cnt++;
    
                if(g[m[s]][m[e]] > t) g[m[s]][m[e]] = g[m[e]][m[s]] = t;
            }
    
            n = cnt;
            Dijkstra();
            if(d[m[end]] == INF) printf("-1\n");
            else printf("%d\n", d[m[end]]);
    
        }
        return 0;
    }


  • 相关阅读:
    react-路由简单封装
    promise 和 async / await
    数据结构 栈 、 队列 、 链表
    ES6 Symbol
    react-react常用包与对应使用
    node-egg的使用
    自我理解与概述-BFC(Block formatting context)
    Git
    MySQL优化技巧
    Shiro
  • 原文地址:https://www.cnblogs.com/freezhan/p/3219052.html
Copyright © 2011-2022 走看看