Update SPFA.cpp

This commit is contained in:
Kirigaya Kazuto 2016-08-22 14:33:48 +08:00 committed by GitHub
parent d7dde95604
commit d308527b16

View File

@ -6,9 +6,9 @@ int d[MAXN];/// distance [ From S to ... ]
int v[MAXN];/// visit
int q[MAXN];/// 基于数组的队列(也可用queue等...)
int mp[MAXN][MAXN]; /// mp[i][j] ???<--> j ?? ?1??
int n;
int n;/// n is the number of max Point .
int spfa(int s,int t)
void spfa(int StartPoint) /// d[i] is the min distance from StartPoint to i ( Both >=1 )
{
memset(d,0x3f,sizeof(d));
memset(v,0,sizeof(v));
@ -16,23 +16,24 @@ int spfa(int s,int t)
for(int i=1;i<MAXN;i++)
d[i]=INF,v[i]=0;*/
int cnt=0;
q[cnt++]=1;
v[1]=1;
d[1]=0;
q[cnt++]=StartPoint;
v[StartPoint]=1;
d[StartPoint]=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])
/// 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] )
{
d[i]=d[c]+mp[c][i];
if(!v[i]) v[i]=1,q[cnt++]=i;
}
}
}
return d[t];
}
}/// End of NameSpace SPFA