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:35:18 +08:00
|
|
|
int mp[MAXN][MAXN]; /// mp[i][j] i<--> j is connected.
|
2016-08-22 14:33:48 +08:00
|
|
|
int n;/// n is the number of max Point .
|
2016-08-22 13:34:27 +08:00
|
|
|
|
2016-08-22 14:33:48 +08:00
|
|
|
void spfa(int StartPoint) /// d[i] is the min distance from StartPoint to i ( Both >=1 )
|
2016-08-22 13:34:27 +08:00
|
|
|
{
|
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;
|
2016-08-22 14:33:48 +08:00
|
|
|
q[cnt++]=StartPoint;
|
|
|
|
v[StartPoint]=1;
|
|
|
|
d[StartPoint]=0;
|
2016-08-22 13:34:27 +08:00
|
|
|
while(cnt>0)
|
|
|
|
{
|
|
|
|
int c=q[--cnt];
|
|
|
|
v[c]=0;
|
|
|
|
for(int i=1;i<=n;i++)
|
|
|
|
{
|
2016-08-22 14:33:48 +08:00
|
|
|
/// Here : if your mp[i][j] use INF as infinite, then use mp[c][i]!=INF.
|
|
|
|
/// Or you may use mp[i][j]!=-1 && d[i] > d[c] + mp[c][i]
|
|
|
|
if( mp[c][i]!=INF && d[i]>d[c]+mp[c][i] )
|
2016-08-22 13:34:27 +08:00
|
|
|
{
|
|
|
|
d[i]=d[c]+mp[c][i];
|
|
|
|
if(!v[i]) v[i]=1,q[cnt++]=i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}/// End of NameSpace SPFA
|