POJ 2387 Shortest Path SPFA implementation

Source: Internet
Author: User

http://poj.org/problem?id=2387

The main problem is to find the shortest way, from N to 1 of the most short-circuit. It is used to familiarize yourself with the SPFA.

First contributed several times WA, the result found is because N,m writes the inverse ....

There is no corner place to familiarize yourself with the SPFA Direct attached code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector >using namespace std; #define M 2009#define INF 0x3f3f3f3fstruct edge{int to,w;}; BOOL Inq[m];vector <edge> g[m];int dis[m];int n,m;void SPFA (int s) {for (int i = 1;i <= n;i++) {dis[        I] = INF;    Inq[i] = false;    } Dis[s] = 0;    Queue<int> Q;    Q.push (s);    Inq[s] = true;        while (!q.empty ()) {s = Q.front ();        Inq[s] = false;        Q.pop ();            for (int i = 0;i < G[s].size (); i++) {int v = g[s][i].to;            int w = G[S][I].W;                if (Dis[v] > Dis[s]+w) {dis[v] = dis[s]+w;                    if (!inq[v]) {Inq[v] = true;                Q.push (v); }}}}}int main () {while (scanf ("%d%d", &m,&n) ==2)//m n is written in reverse and contributed several times WA {for (i NT i = 1;i <= n;i++) g[i].Clear ();            for (int i = 0;i < m;i++) {int a,b,c;            scanf ("%d%d%d", &a,&b,&c);            Edge e;            e.to = B;E.W = C;            G[a].push_back (e);            E.to = A;  G[b].push_back (e);        The non-directional graph changes into two-way.        } SPFA (n);    printf ("%d\n", dis[1]); } return 0;}


POJ 2387 Shortest Path SPFA implementation

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.