Analysis: template problem, direct template can be.
#include <iostream> #include <queue>using namespace std;int u[2002];int v[2002];int w[2002];bool vis[202]; int D[202];int first[202];int next[2002];void Init (int n,int m) {int i; for (i=0;i<n;i++) {vis[i]=false; First[i]=-1; } for (i=0;i<m;i++) next[i]=-1;} void SPFA (int n,int s)//flag indicates whether the current process is a reverse-established diagram or a start graph, false reverse, true start {queue<int> q; int i,x,y; for (i=0;i<n;i++) d[i]= (i==s)? 0:0x7fffffff; Initialize yourself to yourself as 0, other to yourself equivalent to infinity Q.push (s); while (!q.empty ()) {X=q.front (); Q.pop (); Vis[x]=false; for (I=first[x];i!=-1;i=next[i]) {y=v[i]; if (D[y]>d[x]+w[i]) {d[y]=d[x]+w[i]; if (!vis[y]) {vis[y]=true; Q.push (y); }}}}}void Read (int m) {int i,a,b; for (i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,& w[i]); U[i]=a; Store forward edge, because it is an v[i]=b graph; Next[i]=first[a]; First[a]=i; W[i+1]=w[i]; Store reverse Edge i++; U[i]=b; V[i]=a; NEXT[I]=FIRST[B]; First[b]=i; }}void Output (int t) {if (D[T]==0X7FFFFFFF) puts ("1"); elseprintf ("%d\n", D[t]);} int main () {int n,m; int s,t; while (scanf ("%d%d", &n,&m) ==2) {Init (n,m+m); M+m is because there is no edge, each to save two Read (m+m); scanf ("%d%d", &s,&t); SPFA (n,s); Output (t); } return 0; }
HDU ACM 1874 unblocked Project continued