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

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

    HDU Today

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


    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 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<map>
     4 #include<string>
     5 #include<cstring>
     6 #define N 155
     7 #define INF 0x1fffffff
     8 using namespace std;
     9 int mp[N][N];
    10 bool p[N];
    11 int dist[N];
    12 void dijk(int s , int n)
    13 {
    14     int i , j , k ;
    15     for( i = 1 ; i <= n ;i++)
    16     {
    17         p[i] = false;
    18         dist[i] = mp[s][i];
    19     }
    20     p[s] = true;
    21     dist[s] = 0;
    22     for(i = 1 ; i < n ; i++)
    23     {
    24         int Min = INF;
    25         int k = 0 ;
    26         for( j = 1 ; j <= n ;j++)
    27         {
    28             if(!p[j]&&dist[j]<Min)
    29             {
    30                 Min = dist[j];
    31                 k = j;
    32             }
    33         }
    34         if(Min==INF) return ;
    35         p[k] = true;
    36         for(j = 1 ; j <= n ;j++)
    37         {
    38             if(!p[j]&&mp[k][j]!=INF&&dist[j]>dist[k]+mp[k][j])
    39                 dist[j] = dist[k]+mp[k][j];
    40         }
    41     }
    42 }
    43 map < string ,int > city;
    44 int main()
    45 {
    46     int n ;
    47     while(~scanf("%d",&n)&&n!=-1)
    48     {
    49         int cnt = 1 ;
    50         city.clear();
    51         string ss, tt;
    52         cin>>ss>>tt;
    53         if(city.find(ss)==city.end())
    54             city[ss] = cnt++;
    55         if(city.find(tt)==city.end())
    56             city[tt] = cnt++;
    57         int l ;
    58         string s , t ;
    59         for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) mp[i][j] = ((i==j) ? 0 : INF);
    60         for(int i = 0 ;i < n ;i++)
    61         {
    62             cin>>s>>t>>l;
    63             if(city.find(s)==city.end())
    64                 city[s] = cnt++;
    65             if(city.find(t)==city.end())
    66                 city[t] = cnt++;
    67             int sid = city[s], tid = city[t];//这样会减少复杂度
    68             //printf("%d %d
    ",sid, tid);
    69             if(mp[sid][tid] > l) mp[sid][tid] = mp[tid][sid] = l;
    70         }
    71 
    72         dijk(city[ss], city.size());
    73         if(dist[city[tt]]!=INF)
    74             printf("%d
    ",dist[city[tt]]);
    75         else printf("-1
    ");
    76     }
    77     return 0 ;
    78 }
  • 相关阅读:
    ELF和a.out文件格式的比较
    vim常用命令
    安装linux各种桌面环境
    使用virt-manager创建和管理虚拟机
    第一天 纪念一下
    i节点,容易被人遗忘的节点
    【Linux】服务器之间的免密登录脚本
    【python】python调用shell方法
    【ansible】ansible部署方式以及部署包
    【AWS】亚马逊云常用服务解释
  • 原文地址:https://www.cnblogs.com/shanyr/p/4671751.html
Copyright © 2011-2022 走看看