zoukankan      html  css  js  c++  java
  • hdu-2544-最短路(dijkstra算法模板)

     题目链接

    题意很清晰,入门级题目,适合各种模板,可用dijkstra, floyd, Bellman-ford, spfa

    Dijkstra链接

    Floyd链接

    Bellman-Ford链接

    SPFA链接

     1 /*
     2     Name:HDU-2544-最短路
     3     Copyright:
     4     Author:
     5     Date: 2018/4/17 10:34:47
     6     Description:
     7     dijkstra模板 
     8 */
     9 #include <cstring>
    10 #include <cstdio>
    11 #include <iostream>
    12 using namespace std; 
    13 const int MAXN = 105;
    14 const int INF = 0x3f3f3f3f;
    15 int dis[MAXN], g[MAXN][MAXN], N, M;
    16 bool v[MAXN];
    17 void dijkstra() {
    18     for (int i=1; i<=N; i++) {
    19         dis[i] = INF;
    20     }
    21     dis[1] = 0;
    22     memset(v, 0, sizeof(v));
    23     for (int i=1; i<=N; ++i)  {
    24         int mark=-1, mindis=INF;
    25         for (int j=1; j<=N; j++) {
    26             if (!v[j] &&dis[j] <mindis) {
    27                 mindis = dis[j];
    28                 mark = j;
    29             }
    30         }
    31         v[mark] = 1;
    32         for (int j=1; j<=N; j++) {
    33             if (!v[j]) {
    34                 dis[j] = min(dis[j], dis[mark] + g[mark][j]);
    35             }
    36         }
    37     }
    38 }
    39 int main()
    40 {
    41 //    freopen("in.txt", "r", stdin);
    42     while (~scanf("%d %d", &N, &M) && (N+M)) {
    43         memset(g, 0x3f, sizeof(g));
    44         for (int i=0; i<M; i++) {
    45             int a, b, c;
    46             scanf("%d %d %d", &a, &b, &c);
    47             if (g[a][b] > c) {
    48                 g[a][b] = g[b][a] = c;
    49             }
    50         }
    51         dijkstra();
    52         cout<<dis[N]<<endl;
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    Node.js 安装配置
    ITerm2配置-让你的mac命令行更加丰富高效
    ECharts 图表工具
    Vue 安装
    element-ui 安装
    mysql高级查询
    数据库第三章 参考
    DML和DQL 总结
    数据库第二章 参考答案
    数据库编程技术 第一章
  • 原文地址:https://www.cnblogs.com/langyao/p/8867630.html
Copyright © 2011-2022 走看看