zoukankan      html  css  js  c++  java
  • HDU_2112(最短路)

    经过锦囊相助,海东集团终于度过了危机,从此,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
    
    
    虽然偶尔会迷路,但是因为有了你的帮助
    **和**从此还是过上了幸福的生活。
    
    ――全剧终――

     1 #include <iostream>
     2 #include <queue>
     3 #include <map>
     4 #include <string.h>
     5 using namespace std;
     6 const int INF = 1234567;
     7 const int MAXN = 12345;
     8 int vis[MAXN];
     9 int dis[MAXN];
    10 int g[MAXN][MAXN];
    11 
    12 void init(){
    13     for(int i = 1; i < 150; i++){
    14         for(int j = 1; j < 150; j++){
    15             if(i == j)
    16                 g[i][j] = 0;
    17             else g[i][j] = INF;
    18         }
    19     }
    20     memset(vis,0,sizeof(vis));
    21 }
    22 
    23 void dij(int v0, int count){
    24     int pos = v0;
    25     for(int i = 1; i <= count; i++){
    26         dis[i] = g[v0][i];
    27     }
    28     vis[pos] = 1;
    29     for(int i = 1; i < count; i++){
    30         int mins = INF;
    31         for(int j = 1; j <= count; j++){
    32             if(!vis[j] && dis[j] < mins){
    33                 pos = j;
    34                 mins = dis[j];
    35             }
    36         }
    37         vis[pos] = 1;
    38         for(int j = 1; j <= count; j++){
    39             if(!vis[j] && dis[j] > dis[pos] + g[pos][j]){
    40                 dis[j] = dis[pos] + g[pos][j];
    41             }
    42         }
    43     }
    44 }
    45 int main(){
    46     int n;
    47     map<string,int> mp;
    48     while(cin >> n && n != -1){
    49         init();
    50         mp.clear();
    51         string str1, str2;
    52         cin >> str1 >> str2;
    53         mp[str1] = 1;
    54         mp[str2] = 2;
    55         int flag = 0;
    56         if(str1 == str2)
    57             flag = 1;
    58         int count = 3;
    59         for(int i = 1; i <= n; i++){
    60             int w;
    61             cin >> str1 >> str2 >> w;
    62             if(!mp[str1])
    63                 mp[str1] = count++;
    64             if(!mp[str2])
    65                 mp[str2] = count++;
    66             if(w < g[mp[str1]][mp[str2]])
    67                 g[mp[str1]][mp[str2]] = g[mp[str2]][mp[str1]] = w;
    68         }
    69         if(flag){
    70             cout << 0 << endl;
    71             continue;
    72         }
    73         dij(1,count - 1);
    74         if(dis[2] == INF)
    75             cout << -1 << endl;
    76         else 
    77             cout << dis[2] << endl;
    78     }
    79 }
  • 相关阅读:
    HDU 1058 Humble Numbers
    HDU 1421 搬寝室
    HDU 1176 免费馅饼
    七种排序算法的实现和总结
    算法纲要
    UVa401 回文词
    UVa 10361 Automatic Poetry
    UVa 537 Artificial Intelligence?
    UVa 409 Excuses, Excuses!
    UVa 10878 Decode the tape
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6840360.html
Copyright © 2011-2022 走看看