码迷,mamicode.com
首页 > 其他好文 > 详细

queue,指针求最短路的区别

时间:2014-10-27 10:39:11      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   for   sp   div   on   log   ad   

这里以spfa为例;//都用邻接表存边;

指针:

int h=1,t=1;
    q[h]=x;
    while(h<=t){
    	int u=q[h];
    	vis[u]=0;
    	for(int i=head[u];i!=-1;i=e[i].next){
    		int v=e[i].to;
    		if(dist[v]>dist[u]+e[i].w){
    			dist[v]=dist[u]+e[i].w;
    			if(!vis[v]){
    				vis[v]=1;
    				q[++t]=v;
    			}
    		}
    	}
    	h++;
    }

queue

void spfa(int x){
	queue<int> q;
	q.push(x);
	memset(vis,true,sizeof(vis));
	while(!q.empty()){
		int u=q.front();
		q.pop;
		vis[u]=0;
		for(int i=head[u];i!=-1;i=e[i].next){
    		int v=e[i].to;
    		if(dist[v]>dist[u]+e[i].w){
    			dist[v]=dist[u]+e[i].w;
    			if(!vis[v]){
    				vis[v]=1;
    				q.push(v);
    			}
    		}
    	}
	} 
}

  

其实也就是出队入队的不同而已;

queue,指针求最短路的区别

标签:style   blog   color   for   sp   div   on   log   ad   

原文地址:http://www.cnblogs.com/polebug/p/4053561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!