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