One of the SPFA algorithm implementations

Source: Internet
Author: User

Problem Description:
Given an n vertex, the forward graph of the M-Edge (some of which may be negative, but no negative ring is guaranteed). Please calculate the shortest path from point 1th to other points (vertices are numbered from 1 to n).

Input format:
First line two integers n, M.
The next M-line, each line has three integers u, V, L, indicating that u to V has an edge of length L.

Output format:
A total of n-1 lines, line I represents the shortest path from point No. 1th to I+1.

Sample input:
7 ·
1 2-1
2 3-1
3 1 2

Sample output:
-1
-2

Data size and conventions:
For 10% of data, n = 2,m = 2.
For 30% of data, n <= 5,m <= 10.
For 100% of data, 1 <= n <= 20000,1 <= m <= 200000,-10000 <= L <= 10000, guaranteeing that all vertices can be reached from any vertex.

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283848586878889909192 #include <stdio.h>#include <queue>#include <string.h>#define Infinite 210000000#define ListEndFlag  -1intnumber_vertex;intnumber_edge;int dist[20010];inthead[20010];struct{    intto, w, next;}edge[200010];voidSPFA(){    // 用于标识顶点是否在队列中    bool isAlreadyInQueue[20010];    // 初始化数据    for(inti = 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)    {        constintx = q.front();        for (inti = head[x]; i != ListEndFlag; i = edge[i].next)        {            constinty = edge[i].to;            constint 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;    }}intmain(){    // 1. 读取顶点数,边数    scanf("%d%d", &number_vertex, &number_edge);    // 2. 设置 flag    memset(head, ListEndFlag, sizeof(head));    // 3. 读取边    for(inti = 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]);    }    return0;}

One of the SPFA algorithm implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.