算法提高之热浪

算法提高之热浪

  • 核心思想:单源最短路 + spfa

    • spfa:如果该点没有在队列中 加入队列 用其更新与其他点距离
  •   #include <iostream>
      #include <cstring>
      #include <algorithm>
      
      using namespace std;
      const int M = 6210 * 2 , N = 2510;
      
      int h[N],ne[M],e[M],w[M],idx;
      int dist[N],q[N];
      bool st[N];
      int n,m,S,T;
      
      void add(int a,int b,int c)
      {
          e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
      }
      void spfa()
      {
          memset(dist,0x3f,sizeof dist);
          int hh=0,tt=1;
          dist[S] = 0;
          q[0] = S;
          st[S] = true;
          while(hh!=tt)
          {
              int t = q[hh++];
              st[t] = false;
              if(hh == N) hh=0;
              for(int i=h[t];~i;i=ne[i])
              {
                  int j = e[i];
                  if(dist[j] > dist[t] + w[i])
                  {
                      dist[j] = dist[t] + w[i];
                      if(!st[j])
                      {
                          q[tt ++ ] = j;
                          if (tt == N) tt = 0;
                          st[j] = true;
                      }
                  }
              }
          }
      }
      int main()
      {
          cin>>n>>m>>S>>T;
          memset(h, -1, sizeof h);
          for(int i=0;i<m;i++)
          {
              int a,b,c;
              cin>>a>>b>>c;
              add(a,b,c),add(b, a, c);
          }
          spfa();
          
          cout<<dist[T]<<endl;
      }
    

相关推荐

  1. 算法提高热浪

    2024-05-15 23:24:09       30 阅读
  2. 算法提高货币系统

    2024-05-15 23:24:09       28 阅读
  3. 算法提高小国王

    2024-05-15 23:24:09       38 阅读
  4. 算法提高魔板

    2024-05-15 23:24:09       23 阅读
  5. 热浪

    2024-05-15 23:24:09       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-15 23:24:09       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-15 23:24:09       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-15 23:24:09       82 阅读
  4. Python语言-面向对象

    2024-05-15 23:24:09       91 阅读

热门阅读

  1. 轻松了解深度学习的几大模型

    2024-05-15 23:24:09       33 阅读
  2. Spring整合Junit(单元测试)

    2024-05-15 23:24:09       29 阅读
  3. Chrome查看User Agent的实战教程

    2024-05-15 23:24:09       34 阅读
  4. Spring Boot中自定义注解来统计方法执行时间

    2024-05-15 23:24:09       33 阅读
  5. Nacos如何进行服务健康检查?

    2024-05-15 23:24:09       31 阅读
  6. 前端面试题日常练-day08 【面试题】

    2024-05-15 23:24:09       31 阅读