dijkstra + heap

View Code
struct PP{
int len,num;
bool operator<(const PP &b) const{
return len > b.len;
}
}dis[30010];
priority_queue<PP> Q;
int vis[30010];
void dijkstra()
{
while(!Q.empty()) Q.pop();
int i,j,k;
PP tmp;
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
dis[i].num=i;
dis[i].len=INF;
}
dis[1].len=0;
dis[1].num=1;
Q.push(dis[1]);
while(!Q.empty())
{
tmp=Q.top();Q.pop();
int now=tmp.num;
if(vis[now]) continue;
vis[now]=1;
for(i=head[now];i!=-1;i=list[i].next)
{
int len=list[i].w;
int to=list[i].v;
if(dis[to].len>dis[now].len+len)
{
dis[to].len=dis[now].len+len;
Q.push(dis[to]);
}
}
}
printf("%d\n",dis[n].len);
}
posted @ 2011-12-09 17:39  Because Of You  Views(592)  Comments(0Edit  收藏  举报