时间复杂度O(ke),k为常数。#include<cstdio> #include<queue> #include<string> #define maxn 1010 #define maxm 2000100 #define TYPE int #define inf 0xffffff using namespace std; struct Graph { TYPE dist[maxn]; bool inqueue[maxn]; int NV,NE; int node[maxn]; struct Edge { int in,next; TYPE val; }edge[maxm]; queue<int>Q; TYPE Spfa(int s,int t) { for(int i=0;i<NV;i++) { dist[i]=inf; inqueue[i]=false; } dist[s]=0; Q.push(s); inqueue[s]=true; while(!Q.empty()) { int k=Q.front(); Q.pop(); inqueue[k]=false; for(int i=node[k];i!=-1;i=edge[i].next) { int child=edge[i].in; if(dist[k]+edge[i].val<dist[child]) { dist[child]=dist[k]+edge[i].val; if(!inqueue[child]) { Q.push(child); inqueue[child]=true; } } } } return dist[t]; } void Insert(int root,int child,TYPE val) //建立链接表 { edge[NE].in=child; edge[NE].val=val; edge[NE].next=node[root]; node[root]=NE++; } void init(int n) //初始化 { NV=n; for(int i=0;i<NV;i++) node[i]=-1; NE=0; } }G;