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

    题目链接

    题意很清晰,入门级题目,适合各种模板,可用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         man-ford 
     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 bellman_ford()
    18 {
    19     int i,j;
    20     memset(dis, 0x3f, sizeof(dis));
    21     dis[1]=0;
    22     bool update=true;
    23     while(update){
    24         update=false;
    25         for(i=1;i<=N;++i){
    26             for(j=1;j<=N;++j){
    27                 if(g[i][j]!=INF&&dis[j]>dis[i]+g[i][j]){
    28                     dis[j]=dis[i]+g[i][j];
    29                     update=true;
    30                 }
    31             }
    32         }
    33         if (!update) return;//优化 
    34     }
    35 }
    36 int main()
    37 {
    38 //    freopen("in.txt", "r", stdin);
    39     while (~scanf("%d %d", &N, &M) && (N+M)) {
    40         memset(g, 0x3f, sizeof(g));
    41         for (int i=0; i<M; i++) {
    42             int a, b, c;
    43             scanf("%d %d %d", &a, &b, &c);
    44             if (g[a][b] > c) {
    45                 g[a][b] = g[b][a] = c;
    46             }
    47         }
    48         bellman_ford();
    49         printf("%d
    ", dis[N]);
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    监控JVM回收
    linux JDK安装
    linux 免登陆设置
    搭建redis集群总结
    redis开启持久化
    搭建redis主从复制,遇到的问题总结
    Redis配置参数说明
    模板模式
    观察者模式
    适配器模式
  • 原文地址:https://www.cnblogs.com/langyao/p/8867645.html
Copyright © 2011-2022 走看看