zoukankan      html  css  js  c++  java
  • 洛谷 1339 最短路

    洛谷 1339 最短路 


    传送门


    裸的最短路问题,,没什么可说的,当作是复习了个模板吧

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 
     5 const int maxn = 6000;
     6 const int maxm = 50000;
     7 int last[maxn], pre[maxm], other[maxm], len[maxm];
     8 int tot = 0;
     9 int dis[maxn], que[maxn], vis[maxn];
    10 int n, m, ts, te;
    11 int x, y, z;
    12 void spfa(int s) {
    13     memset(dis, 127, sizeof(dis));
    14     memset(que, 0, sizeof(que));
    15     dis[s] = 0;
    16     que[1] = s;
    17     int h = 0, t = 1;
    18     while (h < t) {
    19         h = (h + 1) % maxn;
    20         int cur = que[h];
    21         vis[cur] = 0;
    22         for (int p = last[cur]; p; p = pre[p]) {
    23             int q = other[p];
    24             if (dis[q] > dis[cur] + len[p]) {
    25                 dis[q] = dis[cur] + len[p];
    26                 if (vis[q] == 0) {
    27                     vis[q] = 1;
    28                     t = (t + 1) % maxn;
    29                     que[t] = q;
    30                 }
    31             }
    32         }
    33     }
    34 }
    35 void add(int x, int y, int z) {
    36     tot++;
    37     pre[tot] = last[x];
    38     last[x] = tot;
    39     other[tot] = y;
    40     len[tot] = z;
    41 }
    42 
    43 int main () {
    44     scanf("%d %d %d %d", &n, &m, &ts, &te);
    45     for (int i = 1; i <= m; i++) {
    46         scanf("%d %d %d", &x, &y, &z);
    47         add(x, y, z);
    48         add(y, x, z);
    49     }
    50     spfa(ts);
    51     printf("%d", dis[te]);
    52     
    53     
    54     return 0;
    55 }
  • 相关阅读:
    程序编译与代码优化 -- 早期(编译期)优化
    Java字节码指令
    知识点
    Openresty配置文件上传下载
    Openresty + nginx-upload-module支持文件上传
    G1日志分析
    Garbage First(G1)垃圾收集器
    Java内存分析工具jmap
    编译JDK1.7
    Java服务CPU占用高问题定位方法
  • 原文地址:https://www.cnblogs.com/CtsNevermore/p/6037824.html
Copyright © 2011-2022 走看看