• hd 2112 HDU Today


    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
     
    spfa算法:
     
      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <queue>
      4 #define inf 0x3f3f3f3f
      5 #define N 1000000
      6 using namespace std;
      7 int vis[200], dis[200], head[200];
      8 int n, cnt, k;
      9 char s1[40], s2[40];
     10 char sta[200][40];
     11 struct note
     12 {
     13     int from, to, cost, next;
     14 }edge[N];
     15 void add(int x, int y, int z)
     16 {
     17     note e = {x, y, z, head[x]};
     18     edge[cnt] = e;
     19     head[x] = cnt++;
     20 }
     21 void spfa()
     22 {
     23     queue<int>q;
     24     memset(vis, 0, sizeof(vis));
     25     memset(dis, inf, sizeof(dis));
     26     q.push(1);
     27     vis[1] = 1;
     28     dis[1] = 0;
     29     while(!q.empty())
     30     {
     31         int u = q.front();
     32         q.pop();
     33         vis[u] = 0;
     34         for(int i = head[u]; i != -1; i = edge[i].next)
     35         {
     36             int v = edge[i].to;
     37             if(dis[v] > dis[u] + edge[i].cost)
     38             {
     39                 dis[v] = dis[u] + edge[i].cost;
     40                 if(!vis[v])
     41                 {
     42                     vis[v] = 1;
     43                     q.push(v);
     44                 }
     45             }
     46         }
     47     }
     48     if(dis[2] == inf)
     49         printf("-1
    ");
     50     else
     51         printf("%d
    ", dis[2]);
     52 }
     53 int main()
     54 {
     55     int a, b, c;
     56     while(~scanf("%d", &n), n != -1)
     57     {
     58         int flag;
     59         k = 2, cnt = 0;
     60         scanf("%s%s", sta[1], sta[2]);
     61         memset(head, -1, sizeof(head));
     62         for(int i = 0; i < n; i++)
     63         {
     64             scanf("%s%s%d", s1, s2, &c);
     65             flag = 1;
     66             for(int i = 1; i <= k; i++)
     67             {
     68                 if(strcmp(s1, sta[i]) == 0)
     69                 {
     70                     flag = 0;
     71                     a = i;
     72                     break;
     73                 }
     74             }
     75             if(flag)
     76             {
     77                 strcpy(sta[++k], s1);
     78                 a = k;
     79             }
     80             flag = 1;
     81             for(int i = 1; i <= k; i++)
     82             {
     83                 if(strcmp(sta[i], s2) == 0)
     84                 {
     85                     b = i;
     86                     flag = 0;
     87                     break;
     88                 }
     89             }
     90             if(flag)
     91             {
     92                 strcpy(sta[++k], s2);
     93                 b = k;
     94             }
     95             add(a, b, c);
     96             add(b, a, c);
     97         }
     98         if(strcmp(sta[1], sta[2]) == 0)
     99         {
    100             printf("0
    ");
    101             continue;
    102         }
    103         spfa();
    104     }
    105     return 0;
    106 }

     floyd算法:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #define INF 0x3f3f3f3f
     4 #define N 160
     5 int dis[N][N];
     6 int n;
     7 char s1[40], s2[40], str[160][40];
     8 void init()
     9 {
    10     for(int i = 1; i < N; i++)
    11     for(int j = 1; j < N; j++)
    12     {
    13         if(j == i)
    14             dis[i][j] = 0;
    15         else
    16             dis[i][j] = INF;
    17     }
    18 }
    19 int min(int x, int y)
    20 {
    21     return x < y ? x : y;
    22 }
    23 void floyd()
    24 {
    25     for(int k = 1; k < N; k++)
    26     for(int i = 1; i < N; i++)
    27     {
    28         if(dis[i][k] != INF)
    29         {
    30             for(int j = 1; j < N; j++)
    31             dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
    32         }
    33     }
    34 }
    35 int main()
    36 {
    37     while(~scanf("%d", &n), n != -1)
    38     {
    39         int k = 2, a, b, t, flag;
    40         init();
    41         scanf("%s%s", s1, s2);
    42         strcpy(str[1], s1);
    43         strcpy(str[2], s2);
    44         while(n--)
    45         {
    46             scanf("%s%s%d", s1, s2, &t);
    47             flag = 1;
    48             for(int i = 1; i <= k; i++)
    49             {
    50                 if(strcmp(str[i], s1) == 0)
    51                 {
    52                     flag = 0;
    53                     a = i;
    54                     break;
    55                 }
    56             }
    57             if(flag)
    58             {
    59                 strcpy(str[++k], s1);
    60                 a = k;
    61             }
    62             flag = 1;
    63             for(int i = 1; i <= k; i++)
    64             {
    65                 if(strcmp(str[i], s2) == 0)
    66                 {
    67                     flag = 0;
    68                     b = i;
    69                     break;
    70                 }
    71             }
    72             if(flag)
    73             {
    74                 strcpy(str[++k], s2);
    75                 b = k;
    76             }
    77             dis[a][b] = dis[b][a] = t;
    78         }
    79         floyd();
    80         if(strcmp(str[1], str[2]) == 0)
    81         {
    82             printf("0
    ");
    83             continue;
    84         }
    85         if(dis[1][2] != INF)
    86             printf("%d
    ", dis[1][2]);
    87         else
    88             printf("-1
    ");
    89     }
    90     return 0;
    91 }
  • 相关阅读:
    python执行线程方法
    Python 多线程教程:并发与并行
    python中遍历文件的3个方法
    docker-compose编写(英文)
    使用Docker构建redis集群--最靠谱的版本
    iptables四个表与五个链间的处理关系
    Docker相关文档
    HTTP 之 Content-Type
    Python之VSCode
    自定义分页
  • 原文地址:https://www.cnblogs.com/digulove/p/4747223.html
Copyright © 2020-2023  润新知