• hihocoder 第二十五周 spfa 最短路


    其实hihocoder里的题目目前大都是模板题啊-。-

    这周的是SPFA,暑假的时候有看过SPFA,不过一直用的都是Dijkstra,感觉spfa要更加简洁一点~~,今天找了一份之前一直都看不太懂所以就没怎么用的模板==,今天耐下心来观摩最终还是看懂了hh。

    ============================================================

    所以以后都用spfa吧~~~速度快&&避开了stl~~,写这个bolg主要是拿来记录模板的

    #include <cstdio>
    #include <iostream>
    #include <queue>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    typedef long long LL;
    
    const int maxn = 191111;//最大点数
    const int maxe = maxn*20;//最大边数
    int head[maxn];//记录点的头
    int to[maxe],cost[maxe],next[maxe];//记录边的两边断点,边长;
    
    int n,m,st,ed,ecnt;//点数,边数,起始点,结束点,边数记录
    
    void init() {//初始化
        memset(head,0,sizeof(head));
        ecnt = 1;
    }
    void add_edge(int u,int v,int c) {//添加边,姿势类似于链表的添加,是单向边,如果 是无向边的话要反向添加一次边
        to[ecnt] = v;cost[ecnt] = c;next[ecnt] = head[u];head[u] =ecnt++;
    }
    LL dis[maxn];//每个点到起始点的最短距离
    bool vis[maxn];//记录该点是否在更新当中的队列中
    
    void SPFA() {
        memset(dis,0x3f3f,sizeof(dis));
        memset(vis,0,sizeof(vis));
        queue<int> que; que.push(st);
        dis[st] = 0;
        while(!que.empty()) {
            int u = que.front(); que.pop();
            vis[u] = false;
            for(int p = head[u];p;p = next[p]) {
                int &v = to[p];
                if(dis[v]==-1||dis[v] > dis[u] + cost[p]) {
                        dis[v] = dis[u] + cost[p];
                        if(!vis[v]) que.push(v);//如果不在更新队列中,将该点加入
                        vis[v] = true;//加入更新队列当中
                }
            }
        }
    }
    int main() {
        scanf("%d%d%d%d",&n,&m,&st,&ed);
        init();
        while(m--) {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b,c);
            add_edge(b,a,c);
        }
        SPFA();
        printf("%d
    ",dis[ed]);
        return 0;
    }
  • 相关阅读:
    stagefright框架 Video Playback的流程
    ubuntu 10.10 安装 无线网卡驱动
    ffmpeg 播放 m3u8 ts 流时 av_read_frame 流程
    错误:expected classname before ‘{’ token
    avcodec_decode_video2 第三个参数 got_picture_ptr 的含义
    ndk 编译 ffmpeg
    Windows Phone 7中用好Silverlight开发利器
    利用Visual Studio 2010流程模板实现Scrum敏捷开发(下)
    VS2010中使用IntelliTrace来进行调试
    在Windows Azure中实现和调试一个WCF服务(下)
  • 原文地址:https://www.cnblogs.com/jusonalien/p/4177360.html
Copyright © 2020-2023  润新知