问题描述:
给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环)。请你计算从1号点到其他点的最短路(顶点从1到n编号)。

输入格式:
第一行两个整数n, m。
接下来的m行,每行有三个整数u, v, l,表示u到v有一条长度为l的边。

输出格式:
共n-1行,第i行表示1号点到i+1号点的最短路。

样例输入:
3 3
1 2 -1
2 3 -1
3 1 2

样例输出:
-1
-2

数据规模与约定:
对于10%的数据,n = 2,m = 2。
对于30%的数据,n <= 5,m <= 10。
对于100%的数据,1 <= n <= 20000,1 <= m <= 200000,-10000 <= l <= 10000,保证从任意顶点都能到达其他所有顶点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <queue>
#include <string.h>
 
#define Infinite 210000000
#define ListEndFlag  -1
 
int number_vertex;
int number_edge;
 
int dist[20010];
int head[20010];
struct{
    int to, w, next;
}edge[200010];
 
void SPFA()
{
    // 用于标识顶点是否在队列中
    bool isAlreadyInQueue[20010];
 
    // 初始化数据
    for (int i = 2; i <= number_vertex; i++)
    {
        dist[i] = Infinite;
        isAlreadyInQueue[i] = false;
    }
 
    dist[1] = 0;
    isAlreadyInQueue[1] = true;
 
    std::queue<int> q;
    q.push(1);
 
    while (q.empty() == false)
    {
        const int x = q.front();
 
        for (int i = head[x]; i != ListEndFlag; i = edge[i].next)
        {
            const int y = edge[i].to;
            const int w = edge[i].w;
 
            if (dist[x] + w < dist[y])
            {
                dist[y] = dist[x] + w;
 
                if (isAlreadyInQueue[y] == false)
                {
                    q.push(y);
                    isAlreadyInQueue[y] = true;
                }
            }
        }
 
        q.pop();
        isAlreadyInQueue[x] = false;
    }
}
 
int main()
{
    // 1. 读取顶点数,边数
    scanf("%d%d", &number_vertex, &number_edge);
 
    // 2. 设置 flag
    memset(head, ListEndFlag, sizeof(head));
 
    // 3. 读取边
    for (int i = 1; i <= number_edge; i++)
    {
        int x, y, w;
        scanf("%d%d%d", &x, &y, &w);
 
        edge[i].to   = y;
        edge[i].w    = w;
        edge[i].next = head[x];
 
        head[x]      = i;
    }
 
    // 4. 执行 SPFA
    SPFA();
 
    // 5. 输出结果
    for (int i = 2; i <= number_vertex; i++)
    {
        printf("%d\n", dist[i]);
    }
 
    return 0;
}

spfa 的算法实现之一的更多相关文章

  1. poj3259Wormholes (Bellman_Ford/SPFA/Floyed算法判断是否存在负环)

    题目链接:http://poj.org/problem?id=3259 题目大意:一个图,有n个顶点,其中有m条边是双向的且权值为为正,w条边是单向的且权值为负,判断途中是否存在负环,如果有输出YES ...

  2. spfa毒瘤算法

    终于知道怎么卡spfa(不优化)这一毒瘤算法了 下面就是造数据代码,点数才1e5,边数379980 随便测了一组数据: count: 831841219(入队次数) 68917.096 ms(足够t到 ...

  3. 几大最短路径算法比较(Floyd & Dijkstra & Bellman-Ford & SPFA)

    几个最短路径算法的比较:Floyd 求多源.无负权边(此处错误?应该可以有负权边)的最短路.用矩阵记录图.时效性较差,时间复杂度O(V^3).       Floyd-Warshall算法(Floyd ...

  4. 最短路径问题的Dijkstra和SPFA算法总结

    Dijkstra算法: 解决带非负权重图的单元最短路径问题.时间复杂度为O(V*V+E) 算法精髓:维持一组节点集合S,从源节点到该集合中的点的最短路径已被找到,算法重复从剩余的节点集V-S中选择最短 ...

  5. SPFA算法学习笔记

    一.理论准备 为了学习网络流,先水一道spfa. SPFA算法是1994年西南交通大学段凡丁提出,只要最短路径存在,SPFA算法必定能求出最小值,SPFA对Bellman-Ford算法优化的关键之处在 ...

  6. 最短路径--SPFA 算法

    适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...

  7. spfa 单源最短路究极算法

    学习博客链接:SPFA 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm.     SPFA算法是西南交通大学段凡丁于1994年发表的.    从名字我 ...

  8. Dijkstra、Bellman_Ford、SPFA、Floyd算法复杂度比较

    参考 有空再更新下用c++, 下面用的Java Dijkstra:适用于权值为非负的图的单源最短路径,用斐波那契堆的复杂度O(E+VlgV) BellmanFord:适用于权值有负值的图的单源最短路径 ...

  9. 单源最短路——SPFA算法(Bellman-Ford算法队列优化)

    spfa的算法思想(动态逼近法):     设立一个先进先出的队列q用来保存待优化的结点,优化时每次取出队首结点u,并且用u点当前的最短路径估计值对离开u点所指向的结点v进行松弛操作,如果v点的最短路 ...

随机推荐

  1. Oracle WebLogic Server 12c 新特性

    美国时间2011年 12月9日,Oracle公司正式发布WebLogic 12c版本,c是cloud的缩写.截止当前(2013年8月)最新版本为Oracle WebLogic Server 12c ( ...

  2. sublime text3 license & nodejs setting

    ----- BEGIN LICENSE ----- sgbteam Single User License EA7E-1153259 8891CBB9 F1513E4F 1A3405C1 A865D5 ...

  3. Locust HTTP client

    Http client 默认是以安全模式运行的,任何由于连接错误.超时或者类似错误引起的异常,都会返回一个空的Response对象,这个请求将会再locust统计中标记为failure,返回的虚拟对象 ...

  4. Comparación para 2019 Nueva Lonsdor K518S y K518ISE

    Comparación para 2019 Nueva Lonsdor K518S y Lonsdor K518ISE: Igual: Capacidades de Immo y cobertura ...

  5. SpringBoot 整合 slf4j 日志打印

    划水时间,记录一下用到的相关slf4j 日志打印,如何实现配置输出.本地保存log日志文件... 我使用的是SpringBoot框架,slf4j 类库已经包含到了 SpringBoot 框架中,所有, ...

  6. python调用另一个.py文件中的类和函数

    同一文件夹下的调用 1.调用函数 A.py文件如下:def add(x,y):    print('和为:%d'%(x+y)) 在B.py文件中调用A.py的add函数如下: import AA.ad ...

  7. 【python】-matplotlib.pylab常规用法

    目的: 了解matplotlib.pylab常规用法 示例 import matplotlib.pylab as pl x = range(10) y = [i * i for i in x] pl. ...

  8. 使用AChartEngine画动态曲线图

    AChartEngine是一个开源的Android图表库,可以用来画折线图.平滑折线图.饼图.直方图等等.使用简单,功能强大. AChartEngine官网:http://www.achartengi ...

  9. IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)

    不多说,直接上干货! 问题详情 运行出现log4j的问题 -classpath "C:\Program Files\Java\jdk1.8.0_66\jre\lib\charsets.jar ...

  10. object与byte[]的相互转换、文件与byte数组相互转换

    转载自   https://blog.csdn.net/scimence/article/details/52233656 object与byte[]互转 /// <summary> // ...