SPFA template vector, spfavector

Source: Internet
Author: User

SPFA template vector, spfavector

Shortest Path for algorithm training
Time Limit: 1.0 s memory limit: 256.0 MB

Problem description

Given n vertices and Directed Graphs of m edges (some edge weights may be negative, but there is no negative ring ). Calculate the shortest path (from vertex 1 to vertex n) from vertex 1 to other ).


Input Format


The first line has two integers, n and m.
In the next m row, each row has three integers u, v, l, indicating that u to v has an edge with a length of l.


Output Format

There are n-1 rows in total. line I indicates the shortest path from Point 1 to point I + 1.


Sample Input

3 3
1 2-1
2 3-1
3 1 2


Sample output

-1
-2


Data scale and conventions


For 10% of the data, n = 2, m = 2.

For 30% of the data, n <= 5, m <= 10.

For 100% of data, 1 <= n <= 20000,1 <= m <= 200000,-10000 <= l <= 10000, to ensure that all other vertices can be reached from any vertex.


Question link: http://lx.lanqiao.org/problem.page? Gpid = T15


Question Analysis: Repeat the vector board of SPFA.


#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;int const MAX = 200005;int const INF = 1 << 30;int n, m;struct EDGE{    int u, v;    int val;}e[MAX << 2];struct NODE{    int v, w;    NODE(int vv, int ww)    {        v = vv;        w = ww;    }};vector <NODE> vt[MAX];int dis[MAX];bool vis[MAX];void SPFA(int v0){    memset(vis, false, sizeof(vis));    for(int i = 1; i <= n; i++)        dis[i] = INF;    dis[v0] = 0;    queue <int> q;    q.push(v0);    while(!q.empty())    {        int u = q.front();        q.pop();        vis[u] = false;        int sz = vt[u].size();        for(int i = 0; i < sz; i++)        {            int v = vt[u][i].v;            int w = vt[u][i].w;            if(dis[v] > dis[u] + w)            {                dis[v] = dis[u] + w;                if(!vis[v])                {                    q.push(v);                    vis[v] = true;                }            }        }    }}int main(){    scanf("%d %d", &n, &m);    for(int i = 0; i < m; i++)        scanf("%d %d %d", &e[i].u, &e[i].v, &e[i].val);    for(int i = 0; i < m; i++)        vt[e[i].u].push_back(NODE(e[i].v, e[i].val));    SPFA(1);    for(int i = 2; i <= n; i++)        printf("%d\n", dis[i]);}


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.