zoukankan      html  css  js  c++  java
  • HDU 2112 HDU Today,最短路径算法,Dijkstra

    题目链接: 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): 13396    Accepted Submission(s): 3144


    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 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
     
    分析:题目意思很简单,求最短路,要注意的是顶点名称都换成了字符串,要重新标记,还有就是起点和终点重合的情况需要考虑。
    问题:用string来存储字符串应该是很方便的,但是不知道为什么全部用string来表示,用cin输入无限超时啊。后面改为字符数组了然后用scanf居然就过了,太坑爹。
     
    代码如下:
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <queue>
      4 #include <cstring>
      5 using namespace std;
      6 
      7 #define maxn 155
      8 #define INF 10000000
      9 int w[maxn][maxn];
     10 
     11 struct node
     12 {
     13     int u, key;
     14     friend bool operator<(node a, node b)
     15     {
     16         return a.key > b.key;
     17     }
     18 };
     19 
     20 bool visited[maxn];
     21 node d[maxn];
     22 priority_queue<node> q;
     23 char stn[maxn][35];
     24 int sno;
     25 void Dijkstra(int s)
     26 {
     27     for(int i = 0; i < sno; i++)
     28     {
     29         d[i].u = i;
     30         d[i].key = INF;
     31         visited[i] = false;
     32     }
     33     d[s].key = 0;
     34     q.push(d[s]);
     35     while(!q.empty())
     36     {
     37         node nd = q.top();
     38         q.pop();
     39         int st = nd.u;
     40         if(visited[st] == true)
     41             continue;
     42         visited[st] = true;
     43         for(int j = 0; j < sno; j++)
     44         {
     45             if(j!=st && !visited[j] && w[st][j]+d[st].key < d[j].key)
     46             {
     47                 d[j].key = w[st][j]+d[st].key;
     48                 q.push(d[j]);
     49             }
     50         }
     51     }
     52 }
     53 
     54 int Find(char s[])
     55 {
     56     for(int i = 0; i < sno; i++)
     57         if(strcmp(stn[i], s)==0)
     58             return i;
     59     return -1;
     60 }
     61 int main()
     62 {
     63     int n, c, x, y;
     64     char st[35], ed[35], a[35], b[35];
     65     while(scanf("%d", &n), n!=-1)
     66     {
     67         for(int i = 0; i <= maxn; i++)
     68             for(int j = i; j <= maxn; j++)
     69                 w[i][j] = w[j][i] = INF;
     70         scanf("%s%s", st, ed);
     71         strcpy(stn[0], st);
     72         strcpy(stn[1], ed);
     73         sno = 2;
     74         while(n--)
     75         {
     76             scanf("%s %s %d", a, b, &c);
     77             x = Find(a);
     78             if(x == -1)
     79             {
     80                 x = sno;
     81                 //stn[sno++] = a;
     82                 strcpy(stn[sno++], a);
     83             }
     84             y = Find(b);
     85             if(y == -1)
     86             {
     87                 y = sno;
     88                 strcpy(stn[sno++], b);
     89             }
     90             if(w[x][y] > c)
     91                 w[x][y] = w[y][x] = c;
     92         }
     93         if(strcmp(st, ed)==0)
     94         {
     95             printf("0
    ");
     96             continue;
     97         }
     98         Dijkstra(0);
     99         if(d[1].key < INF)
    100             printf("%d
    ", d[1].key);
    101         else
    102             printf("-1
    ");
    103     }
    104     return 0;
    105 }
    View Code
  • 相关阅读:
    SSIS Error:Package Validation Error. SSIS Error Code DTS_E_OLEDBERROR. .Error code: 0x80040E37. An OLE DB record is available. Hresult: 0x80040E37
    MDS
    oracle11g rac静默安装+racADG部署搭建
    oracle 19c 单机ADG部署
    对索引组织表以及簇表的一些理解
    对表的连接的总结
    全局临时表
    对分区表的一些总结
    对临时表空间的一些常用查询
    linux 常用命令总结
  • 原文地址:https://www.cnblogs.com/dzkang2011/p/3812927.html
Copyright © 2011-2022 走看看