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

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=2112  

    HDU Today

    Description

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
    这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
    徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
    请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。

    Input

    输入数据有多组,每组的第一行是公交车的总数$N (0 leq N leq 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

    最短路。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<string>
     8 #include<queue>
     9 #include<map>
    10 using std::map;
    11 using std::cin;
    12 using std::cout;
    13 using std::endl;
    14 using std::find;
    15 using std::sort;
    16 using std::pair;
    17 using std::vector;
    18 using std::string;
    19 using std::multimap;
    20 using std::priority_queue;
    21 #define pb(e) push_back(e)
    22 #define sz(c) (int)(c).size()
    23 #define mp(a, b) make_pair(a, b)
    24 #define all(c) (c).begin(), (c).end()
    25 #define iter(c) decltype((c).begin())
    26 #define cls(arr,val) memset(arr,val,sizeof(arr))
    27 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    28 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    29 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    30 const int N = 20010;
    31 typedef unsigned long long ull;
    32 struct P {
    33     int w, v;
    34     P(int i = 0, int j = 0) :w(i), v(j) {}
    35     inline bool operator<(const P &a) const {
    36         return w > a.w;
    37     }
    38 };
    39 struct Node { int to, w, next; };
    40 struct Dijkstra {
    41     Node G[N];
    42     map<string, int> A;
    43     int start, end, tot, dist[210], head[N];
    44     inline void init() {
    45         tot = 0, A.clear();
    46         cls(head, -1), cls(dist, 0x3f);
    47     }
    48     inline void add_edge(int u, int v, int w) {
    49         G[tot] = { v, w, head[u] }; head[u] = tot++;
    50     }
    51     inline void built(int n) {
    52         char str[4][32];
    53         int u, v, w, k = 1;
    54         scanf("%s %s", str[0], str[1]);
    55         A[str[0]] = k++, A[str[1]] = k++;
    56         rep(i, n) {
    57             scanf("%s %s %d", str[2], str[3], &w);
    58             if (A.find(str[2]) == A.end()) A[str[2]] = k++;
    59             if (A.find(str[3]) == A.end()) A[str[3]] = k++;
    60             u = A[str[2]], v = A[str[3]];
    61             add_edge(u, v, w), add_edge(v, u, w);
    62         }
    63         start = A[str[0]], end = A[str[1]];
    64     }
    65     inline void dijkstra() {
    66         dist[start] = 0;
    67         priority_queue<P> q;
    68         q.push(P(0, start));
    69         while (!q.empty()) {
    70             P t = q.top(); q.pop();
    71             int u = t.v;
    72             if (dist[u] < t.w) continue;
    73             for (int i = head[u]; ~i; i = G[i].next) {
    74                 int &w = dist[G[i].to];
    75                 if (w > dist[u] + G[i].w) {
    76                     w = dist[u] + G[i].w;
    77                     q.push(P(w, G[i].to));
    78                 }
    79             }
    80         }
    81         printf("%d
    ", dist[end] == (int)0x3f3f3f3f ? -1 : dist[end]);
    82     }
    83 }go;
    84 int main() {
    85 #ifdef LOCAL
    86     freopen("in.txt", "r", stdin);
    87     freopen("out.txt", "w+", stdout);
    88 #endif
    89     int n;
    90     while (~scanf("%d", &n) && n != -1) {
    91         go.init();
    92         go.built(n);
    93         go.dijkstra();
    94     }
    95     return 0;
    96 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    overlapped
    overlapped编程
    Oracle DB 使用子查询来解决查询
    OCP-1Z0-051-V9.02-132题
    OCP-1Z0-051-V9.02-131题
    OCP-1Z0-051-V9.02-130题
    OCP-1Z0-051-V9.02-129题
    OCP-1Z0-051-V9.02-128题
    OCP-1Z0-051-V9.02-127题
    OCP-1Z0-051-V9.02-126题
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4649308.html
Copyright © 2011-2022 走看看