2016-08-22 14:09:19 +08:00
|
|
|
/** SPFA 单源最短路径算法 不支持负环*/
|
2016-08-22 13:34:27 +08:00
|
|
|
namespace SPFA
|
|
|
|
{
|
2016-08-22 14:09:19 +08:00
|
|
|
const int MAXN = 1005;
|
|
|
|
int d[MAXN];/// distance [ From S to ... ]
|
2016-08-22 13:34:27 +08:00
|
|
|
int v[MAXN];/// visit
|
|
|
|
int q[MAXN];/// 基于数组的队列(也可用queue等...)
|
2016-08-22 14:09:19 +08:00
|
|
|
int mp[MAXN][MAXN]; /// mp[i][j] ???<--> j ?? ?1??
|
|
|
|
int n;
|
2016-08-22 13:34:27 +08:00
|
|
|
|
|
|
|
int spfa(int s,int t)
|
|
|
|
{
|
2016-08-22 14:09:19 +08:00
|
|
|
memset(d,0x3f,sizeof(d));
|
|
|
|
memset(v,0,sizeof(v));
|
|
|
|
/*
|
|
|
|
for(int i=1;i<MAXN;i++)
|
|
|
|
d[i]=INF,v[i]=0;*/
|
2016-08-22 13:34:27 +08:00
|
|
|
int cnt=0;
|
|
|
|
q[cnt++]=1;
|
|
|
|
v[1]=1;
|
|
|
|
d[1]=0;
|
|
|
|
while(cnt>0)
|
|
|
|
{
|
|
|
|
int c=q[--cnt];
|
|
|
|
v[c]=0;
|
|
|
|
for(int i=1;i<=n;i++)
|
|
|
|
{
|
|
|
|
if(mp[c][i]!=-1&&d[i]>d[c]+mp[c][i])
|
|
|
|
{
|
|
|
|
d[i]=d[c]+mp[c][i];
|
|
|
|
if(!v[i]) v[i]=1,q[cnt++]=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d[t];
|
|
|
|
}
|
|
|
|
|
|
|
|
}/// End of NameSpace SPFA
|