_Never_

I walk slowly,but I will never stop.

导航

SPFA

//------------SPFA-------------
#define MOVE(x) (x=(x+1)%n)
int map[N][N];
int queue[N],front,rear;
int dist[N];
bool flag[N];
int record[N];

bool SPFA(int n,int s)
{
int i,u;
front
=rear=0;
for(i=1;i<=n;i++)
{
dist[i]
=map[s][i];
flag[i]
=false;
if(dist[i]!=MAXDIST)
{
queue[MOVE(rear)]
=i;
flag[i]
=true;
record[i]
++;
}
}
while(front!=rear)
{
u
=queue[MOVE(front)];
flag[u]
=false;
for(i=1;i<=n;i++)
{
if(dist[u]+map[u][i]<dist[i])
{
dist[i]
=dist[u]+map[u][i];
if(!flag[i])
{
queue[MOVE(rear)]
=i;
if(++record[i]>n) return false;//你说这行写的对不?
}
}
}
}

return true;
}

posted on 2011-09-14 14:52  _Never_  阅读(193)  评论(0编辑  收藏  举报