zoukankan      html  css  js  c++  java
  • hdu-2112 HDU Today---dijkstra+标号

    题目链接:

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

    题目大意:

    求起点到终点的最短路

    解题思路:

    对地名进行编号即可

    然后直接dijkstra算法

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int maxn = 1000 + 10;
     5 const int INF = 0x3f3f3f3f;
     6 int Map[maxn][maxn];
     7 int v[maxn], d[maxn];
     8 int n, m;
     9 set<string>cnt;
    10 map<string, int>G;
    11 int GetID(string s)
    12 {
    13     if(cnt.count(s))return G[s];
    14     cnt.insert(s);
    15     return G[s] = cnt.size();
    16 }
    17 void dijkstra(int s, int t)
    18 {
    19     memset(v, 0, sizeof(v));
    20     for(int i = 0; i <= n; i++)d[i] = (i == s ? 0 : INF);
    21     for(int i = 0; i < n; i++)
    22     {
    23         int x = -1, m = INF;
    24         for(int i = 1; i <= n; i++)if(!v[i] && d[i] <= m)m = d[x = i];
    25         v[x] = 1;
    26         for(int i = 1; i <= n; i++)
    27         {
    28             if(d[i] > d[x] + Map[x][i])
    29             {
    30                 d[i] = d[x] + Map[x][i];
    31             }
    32         }
    33     }
    34     if(d[t] == INF)d[t] = -1;
    35     cout<<d[t]<<endl;
    36 }
    37 
    38 int main()
    39 {
    40     while(scanf("%d", &n) != EOF && n != -1)
    41     {
    42         int u, v, w;
    43         string s1,s2;
    44         cnt.clear();
    45         G.clear();
    46         cin >> s1 >> s2;
    47         int s = GetID(s1);
    48         int t = GetID(s2);
    49         memset(Map, INF, sizeof(Map));
    50         while(n--)
    51         {
    52             cin >> s1 >> s2 >> w;
    53             Map[GetID(s1)][GetID(s2)] = Map[GetID(s2)][GetID(s1)] = min(Map[GetID(s1)][GetID(s2)], w);
    54         }
    55         n = cnt.size();
    56         dijkstra(s, t);
    57     }
    58 }
  • 相关阅读:
    webstorm
    呐,这是某蒟蒻幼稚的博客 ~~Welcome
    CSP-S 2021 补题记录
    CSP-S 2021 游记
    Tarjan 算法小结
    FHQ Treap 浅析
    2048游戏 (C++ Windows)
    线段树 算法分析
    树状数组 算法分析
    数学期望(ξ) 浅析
  • 原文地址:https://www.cnblogs.com/fzl194/p/8907759.html
Copyright © 2011-2022 走看看